Switch statement

Hello!
I am trying put a string to the file "test.dat" using "switch". File exist and empty.
Program shows
Enter a string (CR to quit)
I start typing the string and program is closed immediately. If the file isn't exist all the same. Program "takes off". What should I change for in my code in order to correct the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream.h>
#include <stdio.h>
#include <cstdlib>
#include <string.h>
#include <conio.h>

int main(void)
{
char str[80];
FILE *fp;
char input;

cout << "Choose one of following actions:\n";
cout << "a. File exist and empty\n"
     << "b. File is not exist";
cin >> input;

switch (input) {
  case 'a':
    fp = fopen("TEST.dat", "r");
    do {
  printf("Enter a string (CR to quit):\n");
  gets(str);
  strcat(str, "\n"); /* add a newline */
  fputs(str, fp);
} while(*str!='\n');
  break;

  case 'b':
    fp = fopen("TEST.dat", "w");
    do {
  printf("Enter a string (CR to quit):\n");
  gets(str);
  strcat(str, "\n"); /* add a newline */
  fputs(str, fp);
} while(*str!='\n');
  break;

  default:
    cout << "Enter a or b" << endl;
    break;
  }

return 0;
}
Last edited on
Hi,

First of all you mixed C++ with C code and that's not a good practice...here is your program written just in C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {
      string str;
      ofstream fp;
      fp.open("TEST.dat");
      cout<<"Enter a string (CR to quit):"<<endl;
      getline(cin,str);
      fp<<str;
      fp.close();
    return 0;
} 

as you can see if the file exist, the content will be replaced by what you enter from input, so you don't need that switch
I start typing the string and program is closed immediately.

The immediate problem is that after cin >> input; there will be a trailing newline character remaining in the input buffer, which causes problems with the subsequent entering of a string. That can be fixed by inserting this just after line 16:
 
    cin.ignore(1000, '\n');
This will ignore up to 1000 characters from cin, or until a the delimiter '\n' is found.

However, there are other problems; at line 20 the file is opened for input but at line 25 the program tries to output to the file.

There is also an unseemly mix of C and C++ style i/o, and the use of outdated headers.

Here's a simplified version of the program, using C++ i/o.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{
    const int SZ = 80;
    char str[SZ];

    ofstream f("TEST.dat");

    while (true) 
    {
        cout << "Enter a string (CR to quit):" << endl;
        cin.getline(str, SZ);
        if (!strlen(str))
            break;                
        f << str << '\n';
    }

    return 0;
}
Thank you so much for help, guys!
Last edited on
Why does not my program work?

Two reasons:
1. there is a trailing '\n' in the input buffer after cin >> input;
2. the program attempts to write output to a file which was opened for input (read only).

But it is necessary to realize algorithm of the problem.

Could you describe what algorithm you are supposed to implement, it isn't clear.
Last edited on
@Chervil. I wrote
But it is necessary to realize algorithm of the problem.
in answer to Darehow post. And a code scheme of the first post is realized now according your recommendations of cin.ignore and replacing fp = fopen("TEST.dat", "r"); to
fp = fopen("TEST.dat", "a");
Thanks again!
Last edited on
Topic archived. No new replies allowed.