Switch cases and file handling

So, for the past two days, I've been trying to wrap my head around this.
I've done searches with google, and learned about new functions being made inside of switch cases, and I've tried something else with a prototype function.

Basically, I'm wanting to write from the ground up a time scheduler with reminder application to help me stay on task while at my work pc, I've got things to do at certain hours and an on screen reminder to switch to something would be nice. I'm wanting to use a text file to store said reminders, and when it reaches a certain time in the day, it will read off that scheduled reminder in something like python having dialog windows to pop to screen.

This is a switch case statement and I'm having trouble getting my .txt file calls and handles to work like they should, but when I put calls to print to screen and carriage returns, those work straight from the prototype calls. A little help with getting it to work would be much appreciated.

TLDR; Also used if/else statements and it still didn't work...

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
  /*
int write(){
    ofstream myfile;
    myfile.open("derp.txt",ios::app);
    string a;
    getline(cin,a);
    myfile<<a<<endl;
    myfile.close();
    return 0;
}
int main(){
    cout<<"Enter your derp: 1 for derp, 2 for nah\n";
    int choice;
    cin>>choice;
    switch(choice)
    {
    case 1:
        cout<<"New selected!\nEnter new derp\n";
        write;
        break;
    case 2:
        cout<<"Nah selected\n";
        break;
    }
}
*/


I've tried this out on Code::Blocks most recent stable release on both Windows 7x64 and Windows 8.1x64. I've tried to do the file handling directly in the switch case blocks and inside if/else statements.

As far as research goes, I've went to codebrother, this site for both file handling and switch case information and geeksforgeeks.
Last edited on
oh. switches are kind of frustrating because they ONLY work on integer types.
your appear to be reading your file as text.
string a;
getline (cin, a);
a is a string. a may contain the text "1" but "1" DOES NOT EQUAL the integer 1.
you can convert it: int blah = atoi(a);
or you can just set up your text file in a known format and read numbers:
int blah << filevariable; //don't use getline, get each piece of information into its own variable approach.

or you can use an if statement chain instead:

if(a == "1")

that is, if I understood your question :)





Last edited on
the getline(cin,a) is taking a user input, and storing it to the string a; call, allowing the user input to be wrote to the text file. In the main function, the switch case calls on the int choice for the 1 or 2 cases to be called. When case 1 is chosen, 'write;' is supposed to be called, but it doesn't write anything to the derp.txt or ask for the user input.

The code block is supposed to work: What do you want to do? 1 to write to the file, or 2 to cancel. Input of 1 should then result in: What do you want to write to file? Input is handled as a string in write prototype and then stored to int a, where it is wrote to the file.

Singling out the write prototype as it's own main function, everything works how it is supposed to but when it's introduced to if/else statements or switch case statements, it doesn't work as if it's ignoring the fact I want to write anything to a file.
Line 19 is wrong. You need brackets () to call this function.
When I add the brackets to line 19, the file handling doesn't get processed still
Check if your file is being opened correctly
1
2
3
4
5
6
7
8
9
myfile.open("derp.txt",ios::app);
if (myfile)
{
    cout << "file opened" << std::endl;
}
else
{
    cout << "error: file could not be opened" << std::endl;
}
When I apply that, it tells me that the file is opened, but still doesn't reach the second call to get user input. It just simply ends the process.

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
int write(){
    ofstream myfile;
    myfile.open("derp.txt",ios::app);
    if(myfile)
    {
        cout<<"File opened";
        string a;
        getline(cin,a);
        myfile<<a<<endl;
        myfile.close();
    }else
    {
        cout<<"Error: File could not be opened\n";
    }
   return 0;
}
int main(){
    cout<<"Enter your derp: 1 for derp, 2 for nah\n";
    int choice;
    cin>>choice;
    if(choice=1){
        write();}
    else{cout<<"Unable to do"<<endl;}
    return 0;
}



I also wanna say thanks for all your guys' help sofar :)
Last edited on
CHOICE = 1 Iin if statement is assignment. == vs = bug.

Show a full program that you can paste here that reproduces your issue.
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
int write(){
    ofstream myfile;
    myfile.open("derp.txt",ios::app);
    if(myfile)
    {
        cout<<"File opened";
        string a;
        getline(cin,a);
        myfile<<a<<endl;
        myfile.close();
    }else
    {
        cout<<"Error: File could not be opened\n";
    }
    return 0;
}
int main(){
    cout<<"Enter your derp: 1 for derp, 2 for nah\n";
    int choice;
    cin>>choice;
    if(choice==1){
        write();}
    else{cout<<"Unable to do"<<endl;}
    return 0;
}


This is what I have as what I'm working on. I've tried the == vs = assignments.

What this is supposed to, or at least attempting to, do is take in user input for choice, with choice entered, it selects appropriate function. Choice 1 is supposed to open a file named "derp.txt" and allow the user to make an entry into it, then finish out the script. None of the file i/o is working, but the cout<< pieces in that write(); function is still called.
That isn't a full program... I cannot compile it as is. I added the necessary lines myself on my own computer, but next time please copy the complete program (the reason I'm being stubborn about this is because, in general, it's possible you're not showing everything, and something you don't think is related actually could be causing the problem).

However, in this case, it isn't too complicated. The problem is the mix of cin >> operator and getline(cin, a). When you do cin >> operator before getline, a newline is kept is the input buffer.

None of the file i/o is working
You should be more specific or more detailed. It, in fact, is successfully writing a file called derp.txt, and the file gets longer every time the program runs (by 1 line each run).

But the problem is getline() is only receiving the newline leftover from the cin>> call. To alleviate this, add a cin.ignore(); call before getline(cin, a);
Last edited on
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
#include <iostream>
#include <fstream>
using namespace std;

int write(){
    ofstream myfile;
    myfile.open("derp.txt",ios::app);
    if(myfile)
    {
        cout<<"File opened, enter text.\n";
        string a;
        cin.ignore();
        getline(cin,a);
        myfile<<a<<endl;
        myfile.close();
    }else
    {
        cout<<"Error: File could not be opened\n";
    }
    return 0;
}
int main(){
    cout<<"Enter your derp: 1 for derp, 2 for nah\n";
    int choice;
    cin>>choice;
    if(choice==1){
        write();}
    else{cout<<"Unable to do"<<endl;}
    return 0;
}


I put that in and it's working now. I only selected what I did because I had a lot of different attempts commented out while trying to find something that worked, sorry.. On my end, derp.txt didn't have anything added to it, as if it was a newly created text file and was a left over from trying the choice 1 section by itself to troubleshoot that. Thank you for the help
On my end, derp.txt didn't have anything added to it, as if it was a newly created text file and was a left over from trying the choice 1 section by itself to troubleshoot that.

You wouldn't see it just by looking at it, but it would actually have 1 newline for every time you ran the program. You might notice it if you highlight it.

But anyway, glad it works now.
Topic archived. No new replies allowed.