Struct and Char not handling space properly

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 7
Reputation: lameassthemity is an unknown quantity at this point 
Solved Threads: 0
lameassthemity lameassthemity is offline Offline
Newbie Poster

Struct and Char not handling space properly

 
0
  #1
Jan 12th, 2009
I'm writing a class planner, where the user is given the opportunity to enter certain information about their classes and have it printed back to them upon request.

The problem I'm having is that whenever I enter any text into the input with a space in it, it returns me to menu().
I also tried using string in my structured array, but then it simply skipped the entire input for that variable.

I'm using a struct in my header file to allow simple input into the array:

  1. struct Classes
  2. {
  3. char ClassName[30];
  4. int ClassNumber;
  5. char DaysMet[20];
  6. int StartTime;
  7. int EndTime;
  8. char Teacher[20];
  9. int Students;
  10. };

and here is the input form:

  1. int Input()
  2. {
  3. system("cls");
  4. if (currentindex > IndexNum)
  5. cout << "Index is full" << endl;
  6. else while (currentindex < IndexNum)
  7. { cout << "Class Name: ";
  8. cin >> Course[currentindex].ClassName;
  9. cout << "Class Number: ";
  10. cin >> Course[currentindex].ClassNumber;
  11. cout << "Days Met: ";
  12. cin >> Course[currentindex].DaysMet;
  13. cout << "Start Time: ";
  14. cin >> Course[currentindex].StartTime;
  15. cout << "End Time: ";
  16. cin >> Course[currentindex].EndTime;
  17. cout << "Teacher: ";
  18. cin >> Course[currentindex].Teacher;
  19. cout << "Students: ";
  20. cin >> Course[currentindex].Students;
  21.  
  22. currentindex++;
  23. }
  24.  
  25. Menu();
  26. return 0;
  27. }

Another way I attempted to get the program to accept input into my char 'ClassName'
  1. cin.getline(Course[currentindex].ClassName, 30)

So, basically anyone who uses this program cannot enter data into the program when the variable has more than one word, for example:

Class Name: "Civics and Economics"
or
Teacher: "Mr. Mathus"

would send the user back to the main menu!

I'm totally clueless as to how to resolve this problem, thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Struct and Char not handling space properly

 
0
  #2
Jan 12th, 2009
Use std::string with getline
  1. #include <string>
  2. ...
  3. std::string myString;
  4. getline(std::cin, myString);
Hope that helps

Chris
Last edited by Freaky_Chris; Jan 12th, 2009 at 6:21 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: lameassthemity is an unknown quantity at this point 
Solved Threads: 0
lameassthemity lameassthemity is offline Offline
Newbie Poster

Re: Struct and Char not handling space properly

 
0
  #3
Jan 13th, 2009
Well, I'm not getting compiler errors now that I'm using the strings properly, but I'm still having trouble accepting input into the strings.

I've tried using a couple methods of input:
(not all at the same time of course)
  1. getline(std::cin, Course[currentindex].ClassName); // when the program runs, it skips this line entirely, jumping to
  2. cout << "Class Number: ";
  3. cin >> Course[currentindex].ClassNumber;
Producing a result in the application looking like this:

Class Name: Class Number:

  1. cin >> Course[currentindex].ClassNumber;
  2. cin.getline(Course[currentindex].ClassName, []); // Doesn't work with strings, obviously.
  1. cin >> Course[currentindex].ClassName; // works for single word answers, but goes to menu() if the input contains a space
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Struct and Char not handling space properly

 
0
  #4
Jan 13th, 2009
do you fdefine your ClassName as a string or a char array?
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: lameassthemity is an unknown quantity at this point 
Solved Threads: 0
lameassthemity lameassthemity is offline Offline
Newbie Poster

Re: Struct and Char not handling space properly

 
0
  #5
Jan 13th, 2009
My header file after your recommendation:
  1. #include <string>
  2. using std::string;
  3.  
  4. struct Classes
  5. {
  6. string ClassName;
  7. int ClassNumber;
  8. string DaysMet;
  9. int StartTime;
  10. int EndTime;
  11. string Teacher;
  12. int Students;
  13.  
  14. };
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Struct and Char not handling space properly

 
0
  #6
Jan 13th, 2009
Ok,this is the else part of your loop
  1. <div class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</div></div> <blockquote style="font-size:11px">cout << "Class Name: ";
  2. cin >> Course[currentindex].ClassName;
  3. cout << "Class Number: ";
  4. cin >> Course[currentindex].ClassNumber;</blockquote> </div>
When you use a cin statement ,the cin stops reading the buffer after it encounters a white space or a newline.So when you read something like this:
"Civics and Economics"
there are spaces and you have got only "Civics" in your
  1. Course[currentindex].ClassName
and the rest part remains in your input stream(inclusive of the newline character).
All these things can be handled by using getline function.
The syntax of using getline with strings is
  1. getline(input_stream,name_of_string)
When you use getline it handlees the the newline ('\n') character (actually converts and appends '\t' to the end of the string) and you don't have to worry about the subsequent lines.
But getline stops reading when it encounters '\n'.So if this is stuck in your input stream getline fails.

I would suggest that you club all your "string input" statements together and all "numerical data " statements together.

But if you don't have a choice then you can discard the '\n' by
  1. cout << "Class Name: ";
  2. cin >> Course[currentindex].ClassName;
  3. char junk;
  4. cin.get(junk);//gets the '\n' character
  5. //even cin.ignore() works
  6. cout << "Class Number: ";
  7. cin >> Course[currentindex].ClassNumber;
Last edited by zalezog; Jan 13th, 2009 at 3:03 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: lameassthemity is an unknown quantity at this point 
Solved Threads: 0
lameassthemity lameassthemity is offline Offline
Newbie Poster

Re: Struct and Char not handling space properly

 
0
  #7
Jan 13th, 2009
Originally Posted by zalezog View Post

  1. //even cin.ignore() works
Perfect! Thanks!

  1.  
  2. {
  3. system("cls");
  4. if (currentindex > IndexNum)
  5. cout << "Index is full" << endl;
  6. else while (currentindex < IndexNum)
  7. {
  8. cout << "Class Name: ";
  9. cin.ignore(); // <----
  10. getline(cin, Course[currentindex].ClassName);
  11. cout << "Class Number: ";
  12. cin >> Course[currentindex].ClassNumber;
  13. cout << "Days Met: ";
  14. cin.ignore(); // <----
  15. getline(cin, Course[currentindex].DaysMet);
  16. cout << "Start Time: ";
  17. cin >> Course[currentindex].StartTime;
  18. cout << "End Time: ";
  19. cin >> Course[currentindex].EndTime;
  20. cout << "Teacher: ";
  21. cin.ignore(); // <----
  22. getline(cin, Course[currentindex].Teacher);
  23. cout << "Students: ";
  24. cin >> Course[currentindex].Students;
  25.  
  26. currentindex++;
  27. }
  28.  
  29. Menu();
  30. return 0;
  31. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Struct and Char not handling space properly

 
0
  #8
Jan 13th, 2009
I advise you use getline() for ALL data types and use string streams to convert them to int etc rather than cin.ignore()!


Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: lameassthemity is an unknown quantity at this point 
Solved Threads: 0
lameassthemity lameassthemity is offline Offline
Newbie Poster

Re: Struct and Char not handling space properly

 
0
  #9
Jan 13th, 2009
Originally Posted by Freaky_Chris View Post
I advise you use getline() for ALL data types and use string streams to convert them to int etc rather than cin.ignore()!
Chris
What do you mean use string steams to convert them?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Struct and Char not handling space properly

 
0
  #10
Jan 13th, 2009
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC