Program crashes even though it compiles correct - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Program crashes even though it compiles correct

sufs_LNDsufs_LND Posts: 23Member
Ok i have a funny problem: Ive made a program which compiles correctly but when I try to run the program with the following input:
Choose operation
1
Which file should be edited?
4 (or any number)

it crashes and wont complete

Here is the code
[CODE]
int main()
{

char bokser[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};

cout << "Choose operation
";
cout << "1. Change file
";


int valg;
cin >> valg;

if (valg == 1) {

cout << "Which file should be edited?" << endl;
char boksnr[1];
cin >> boksnr;


char *fileending;
char filename[10];
strcpy (filename,boksnr);
strcpy (fileending,".txt");
strncat (filename,fileending,5);


char tekst[20];
cout << "Type filecontent";
cin >> tekst;

ofstream myfile;
myfile.open (filename);
myfile << tekst;
myfile.close();
}
else { cout << "Wrong input


"; }
system("pause");
return 0;
}
[/CODE]
Here is the funny part: If i remove the following scentence
[code]
cout << "Choose operation
";
cout << "1. Change file
";


int valg;
cin >> valg;

if (valg == 1) {
[/code]
the program does not crash :S Can anyone see what im doing wrong or is it just my computer?

Im using DEV-c++ on windows XP PRO SP2

oh and btw, the program selects a .txt file in a range from 0-20 and lets you change the content.

Comments

  • IDKIDK Posts: 1,784Member
    : Ok i have a funny problem: Ive made a program which compiles correctly but when I try to run the program with the following input:
    : Choose operation
    : 1
    : Which file should be edited?
    : 4 (or any number)
    :
    : it crashes and wont complete
    :
    : Here is the code
    : [CODE]
    : int main()
    : {
    :
    : char bokser[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    :
    : cout << "Choose operation
    ";
    : cout << "1. Change file
    ";
    :
    :
    : int valg;
    : cin >> valg;
    :
    : if (valg == 1) {
    :
    : cout << "Which file should be edited?" << endl;
    : char boksnr[1];
    : cin >> boksnr;
    :
    :
    : char *fileending;
    : char filename[10];
    : strcpy (filename,boksnr);
    : strcpy (fileending,".txt");
    : strncat (filename,fileending,5);
    :
    :
    : char tekst[20];
    : cout << "Type filecontent";
    : cin >> tekst;
    :
    : ofstream myfile;
    : myfile.open (filename);
    : myfile << tekst;
    : myfile.close();
    : }
    : else { cout << "Wrong input


    "; }
    : system("pause");
    : return 0;
    : }
    : [/CODE]
    : Here is the funny part: If i remove the following scentence
    : [code]
    : cout << "Choose operation
    ";
    : cout << "1. Change file
    ";
    :
    :
    : int valg;
    : cin >> valg;
    :
    : if (valg == 1) {
    : [/code]
    : the program does not crash :S Can anyone see what im doing wrong or is it just my computer?
    :
    : Im using DEV-c++ on windows XP PRO SP2
    :
    : oh and btw, the program selects a .txt file in a range from 0-20 and lets you change the content.
    :

    It shouln't run at all...

    : char *fileending;
    : strcpy (fileending,".txt");

    This should make it crash, but you where lucky before...

    If you don't get it, reply.
  • sufs_LNDsufs_LND Posts: 23Member
    : : Ok i have a funny problem: Ive made a program which compiles correctly but when I try to run the program with the following input:
    : : Choose operation
    : : 1
    : : Which file should be edited?
    : : 4 (or any number)
    : :
    : : it crashes and wont complete
    : :
    : : Here is the code
    : : [CODE]
    : : int main()
    : : {
    : :
    : : char bokser[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    : :
    : : cout << "Choose operation
    ";
    : : cout << "1. Change file
    ";
    : :
    : :
    : : int valg;
    : : cin >> valg;
    : :
    : : if (valg == 1) {
    : :
    : : cout << "Which file should be edited?" << endl;
    : : char boksnr[1];
    : : cin >> boksnr;
    : :
    : :
    : : char *fileending;
    : : char filename[10];
    : : strcpy (filename,boksnr);
    : : strcpy (fileending,".txt");
    : : strncat (filename,fileending,5);
    : :
    : :
    : : char tekst[20];
    : : cout << "Type filecontent";
    : : cin >> tekst;
    : :
    : : ofstream myfile;
    : : myfile.open (filename);
    : : myfile << tekst;
    : : myfile.close();
    : : }
    : : else { cout << "Wrong input


    "; }
    : : system("pause");
    : : return 0;
    : : }
    : : [/CODE]
    : : Here is the funny part: If i remove the following scentence
    : : [code]
    : : cout << "Choose operation
    ";
    : : cout << "1. Change file
    ";
    : :
    : :
    : : int valg;
    : : cin >> valg;
    : :
    : : if (valg == 1) {
    : : [/code]
    : : the program does not crash :S Can anyone see what im doing wrong or is it just my computer?
    : :
    : : Im using DEV-c++ on windows XP PRO SP2
    : :
    : : oh and btw, the program selects a .txt file in a range from 0-20 and lets you change the content.
    : :
    :
    : It shouln't run at all...
    :
    : : char *fileending;
    : : strcpy (fileending,".txt");
    :
    : This should make it crash, but you where lucky before...
    :
    : If you don't get it, reply.
    :
    TY, I changed it to char fileending[5] and it worked :)
  • IDKIDK Posts: 1,784Member
    : : : Ok i have a funny problem: Ive made a program which compiles correctly but when I try to run the program with the following input:
    : : : Choose operation
    : : : 1
    : : : Which file should be edited?
    : : : 4 (or any number)
    : : :
    : : : it crashes and wont complete
    : : :
    : : : Here is the code
    : : : [CODE]
    : : : int main()
    : : : {
    : : :
    : : : char bokser[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    : : :
    : : : cout << "Choose operation
    ";
    : : : cout << "1. Change file
    ";
    : : :
    : : :
    : : : int valg;
    : : : cin >> valg;
    : : :
    : : : if (valg == 1) {
    : : :
    : : : cout << "Which file should be edited?" << endl;
    : : : char boksnr[1];
    : : : cin >> boksnr;
    : : :
    : : :
    : : : char *fileending;
    : : : char filename[10];
    : : : strcpy (filename,boksnr);
    : : : strcpy (fileending,".txt");
    : : : strncat (filename,fileending,5);
    : : :
    : : :
    : : : char tekst[20];
    : : : cout << "Type filecontent";
    : : : cin >> tekst;
    : : :
    : : : ofstream myfile;
    : : : myfile.open (filename);
    : : : myfile << tekst;
    : : : myfile.close();
    : : : }
    : : : else { cout << "Wrong input


    "; }
    : : : system("pause");
    : : : return 0;
    : : : }
    : : : [/CODE]
    : : : Here is the funny part: If i remove the following scentence
    : : : [code]
    : : : cout << "Choose operation
    ";
    : : : cout << "1. Change file
    ";
    : : :
    : : :
    : : : int valg;
    : : : cin >> valg;
    : : :
    : : : if (valg == 1) {
    : : : [/code]
    : : : the program does not crash :S Can anyone see what im doing wrong or is it just my computer?
    : : :
    : : : Im using DEV-c++ on windows XP PRO SP2
    : : :
    : : : oh and btw, the program selects a .txt file in a range from 0-20 and lets you change the content.
    : : :
    : :
    : : It shouln't run at all...
    : :
    : : : char *fileending;
    : : : strcpy (fileending,".txt");
    : :
    : : This should make it crash, but you where lucky before...
    : :
    : : If you don't get it, reply.
    : :
    : TY, I changed it to char fileending[5] and it worked :)
    :


    Another thing...
    Since bobsnr is only one char, it can't have a null after it... so it would crash also to do the following
    [code]
    char fileending[5];
    char filename[10];
    strcpy (filename,boksnr);
    strcpy (fileending,".txt");
    strncat (filename,fileending,5);
    [/code]
    I would write it like this:
    [code]
    char *fileending=".txt";
    char filename[10];
    filename[0] = *boksnr;
    filename[1] = 0;
    strcat (filename,fileending);
    [/code]

    It could be further optimised to:
    [code]
    char* filename[6];
    *filename=*boksnr;
    (*int)(filename+1) = (int)(*".txt");
    filename[5]=0;
    //hmm, I'm not sure if it works right now...
    //but it could be rewritten to work the way I
    //intended... It also needs int to be 32-bit
    [/code]

Sign In or Register to comment.