Thread: ever just wanna bang your head against the desk?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    Unhappy ever just wanna bang your head against the desk?


    Okay the idea is to have the user choose 1 of 4 potential slots. The files do exist on the hard drive in the correct directory as 1.txt, 2.txt, 3.txt and 4.txt. All I want to do is let the user select which file to edit, type in a message, and have the program write to that file.

    The program compiles and runs fine, but its not doing, what looks to me, like an obvious routine even a laymen (me) couldnt screw up. I understand that i havent included 2-4 in the selection process yet, and thats because all this does is prompt for a choice, displays "Enter a message:" and then abruptly exits. getline() isn't coming up to let me type in the message.

    This can't be that hard! What the crap?

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib> // For EXIT_FAILURE, EXIT_SUCCESS
    
    using namespace std;
    
    int main()
    {
        string message;
        int slot;
        //create a name for the file
        cout << "Select a slot:" << endl;
        cout << "1 | 2 | 3 | 4" << endl;
        cin >> slot;       
            
        if(slot == 1) {
        ofstream SaveFile("1.txt",ios::out|ios::app);
        // Get message from user
        cout << "Enter a message: " ;
        getline(cin,message);
        // Write/append message to end of file
        SaveFile << message << endl;
        }
    // else system("pause");
    return EXIT_SUCCESS;
    }
    Last edited by osiris_99; 02-22-2005 at 03:42 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Fix your identation. Everything done inside the if body should be deeper then the if.
    2) What happens if you put in
    Code:
    else
        cout<<"Invalid slot value: "<<slot<<endl;
    3) Please use thread titles at describe your problem. "ever just wanna bang your head against the desk?" tells us how you feel but tells us nothing about the problem.
    4) Please refrain from cursing.

    Edit: The problem steams from the fact a newline character is being left in the buffer after cin >> slot; which is then picked up by getline(). You can "fix" this by putting a cin.ignore() right after your cin >> slot;

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    The else statement does work fine.

    I just can't figure out why, when i select "1", the program displays the message "Enter a message:", and then exits. Does getstring() work inside an if statement? If I remove the if statement with //'s, the program works fine and actually writes to the file.

    Im just totally stumped.
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib> // For EXIT_FAILURE, EXIT_SUCCESS
    
    using namespace std;
    
    int main()
      {
        string message;
        int slot;
        //create a name for the file
        cout << "Select a slot:" << endl;
        cout << "1 | 2 | 3 | 4" << endl;
        cin >> slot;       
           
        if(slot == 1) {
          ofstream SaveFile("1.txt",ios::out|ios::app);
          // Get message from user
          cout << "Enter a message: " ;
          getline(cin,message);
          // Write/append message to end of file
          SaveFile << message << endl;
        }
        else
        cout<<"Invalid slot value: "<< slot <<endl;
      return EXIT_SUCCESS;
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Please read the edit portion of my post.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    Man I would've never gotten that.
    You're good. Thank you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 08:33 AM
  5. GIvE iT tO mE NOwWwwWww bABy!!!!!!!! wooooooooo
    By Xterria in forum Party Board
    Replies: 12
    Last Post: 07-14-2003, 07:03 AM