Thread: C++ simple problem that I can't figure out

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    C++ simple problem that I can't figure out

    Here is my source code: (I assure you this is not homework, I am teaching myself programming, and thought I should write a helpful program to learn from.)

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <conio.h>
    #include <sstream>
    
    
    using namespace std;
    
    void WelcomeScreen();
    void AddItems();
    
    char nameOfClass[25];
    char descriptionOfAssignment[250];
    char dueDate[20];
    int numAssn; //number of assignments
    //int x = 0;
    
    
    
    int main()
    {
        WelcomeScreen();
        AddItems();
        
        cout << "\t\t\tHOMEWORK ASSIGNMENT PLANNER" << endl;
        cout << "\t\t\tPROGRAMMED BY DAMON VESSEY" << endl;
        cout << "##########################################################################" << endl;
        cout << endl << endl << endl;
        cout << "This program has been successfully terminated." << endl;
        getch();
        
        
      return 0;
    }
      
        
    void WelcomeScreen()
    {
        system("cls");
        cout << "\t\t\tHomework Assignment and Test Planner" << endl;
        getch();
        return;
    }
        
    
    void AddItems()
    {         
              int x = 0;
        ofstream myfileout;
         myfileout.open( "HwAssignment.txt", ios::out );
         
         cout << "How many homework assignments do you have?" << endl;
         cin >> numAssn;
         
         do {
             x++;
         cout << "What is the name of the class for which this homework assignment is for?" << endl;
         cin.getline( nameOfClass, 25);
              myfileout << "[" << nameOfClass << "]";
         
         cout << "Write a description of what your assignment or test covers." << endl;
         cin.getline( descriptionOfAssignment, 250);
              myfileout << " - " << descriptionOfAssignment;
         
         cout << "What is the date of which your assignment is due or the day you take your test?" << endl;
         cin.getline( dueDate, 20);
              myfileout << " - (Due: " << dueDate << ")";
              
              myfileout << endl;
              } while (x < numAssn);
         
         myfileout.close();
         return;
    }
    I can't figure out why when the program asks "how many homework assignments does it have?", that after the user answers, it skips the question "What is the name of the class for which this homework assignment is for?", and does not even allow the user to input anything, and goes straight to the question "Write a description of what your assignment or test covers." Does anyone know why this happens? I have had this happen to me on several of my other programs, but accidently fixed them somehow, but I want to understand why this happens. Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cin >> numAssn;
    This leaves a newline on the input stream

    > cin.getline( nameOfClass, 25);
    This uses the newline left behind by the previous input method.

    Mixing input methods is a bad idea. You could try say
    cin.ignore();
    but for me, the simplest answer is to always use getline to read the input, then do whatever you need to convert the result yourself. Using string streams makes this a lot easier to do.

    By the time you've made the code robust enough to deal with
    "please enter a number", and your user types in
    "I'm an idiot"
    there isn't a lot of difference between what seems like a "short" way and a "long" way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28
    i just replace all
    Code:
    cin.getLine( variable , # );
    into
    Code:
    cin >> variable;
    and my output file looks fine.

    i guess they don't get along with each other.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The difference is that cin >> variable; won't include whitespace, so if your name or description or due date have spaces in them it won't work right.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3

    Thank you everyone

    Thank you very much everyone for replying (and with relevance). Now I understand how my program works. Thanks again everyone!

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    char nameOfClass[25];
    char descriptionOfAssignment[250];
    char dueDate[20];
    int numAssn; //number of assignments
    It is best to not get into the habbit of having to many things global. Try to keep data as local to the functions as you can, and pass them by value/reference when needed.
    Double Helix STL

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a sin to use char with cin.
    Use std::string and std::getline instead. This avoids nasty buffer overruns and you don't need to worry about the size of the buffer either.
    (Although I don't remember if std::getline includes or ignores [leaves] whitespace in the input stream.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I don't remember if std::getline includes or ignores [leaves] whitespace in the input stream.
    It reads in and includes all whitespace in the resulting string except for the delimiter (which is a newline by default). It reads and discards the delimiter. Basically that means that it reads and includes spaces and tabs and stops at a newline, then removes the newline from the stream.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    3
    when using strings, is the user allowed to input spaces?

    By the way thank you everyone for replying, as these discussions are very helpful, as I have been reading many C++ books, but none of them talk about the problems associated with the things you all have mentioned.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    With my proposed solution, then yes, it should read everything you enter, including spaces. That's why it's so convenient!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  2. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 04:07 PM
  3. Simple code :: Cant figure out the problem...
    By pritin in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:02 AM
  4. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  5. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM