943,734 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 659
  • C++ RSS
Jan 12th, 2009
0

Struct and Char not handling space properly

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:

c++ Syntax (Toggle Plain Text)
  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'
c++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lameassthemity is offline Offline
7 posts
since May 2008
Jan 12th, 2009
0

Re: Struct and Char not handling space properly

Use std::string with getline
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

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)
c++ Syntax (Toggle Plain Text)
  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:

c++ Syntax (Toggle Plain Text)
  1. cin >> Course[currentindex].ClassNumber;
  2. cin.getline(Course[currentindex].ClassName, []); // Doesn't work with strings, obviously.
c++ Syntax (Toggle Plain Text)
  1. cin >> Course[currentindex].ClassName; // works for single word answers, but goes to menu() if the input contains a space
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lameassthemity is offline Offline
7 posts
since May 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

do you fdefine your ClassName as a string or a char array?
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

My header file after your recommendation:
c++ Syntax (Toggle Plain Text)
  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. };
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lameassthemity is offline Offline
7 posts
since May 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

Ok,this is the else part of your loop
C++ Syntax (Toggle Plain Text)
  1. <div> <div class="quote"> <div class="quote_header"> Quote ... </div> <blockquote class="quote_body"> cout << "Class Name: ";
  2. cin >> Course[currentindex].ClassName;
  3. cout << "Class Number: ";
  4. cin >> Course[currentindex].ClassNumber; </blockquote> </div> </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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

Click to Expand / Collapse  Quote originally posted by zalezog ...

c++ Syntax (Toggle Plain Text)
  1. //even cin.ignore() works
Perfect! Thanks!

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lameassthemity is offline Offline
7 posts
since May 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

I advise you use getline() for ALL data types and use string streams to convert them to int etc rather than cin.ignore()!


Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lameassthemity is offline Offline
7 posts
since May 2008
Jan 13th, 2009
0

Re: Struct and Char not handling space properly

Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: diff command (unix command) implementation in c++
Next Thread in C++ Forum Timeline: Simple user defined loops?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC