| | |
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:
Solved Threads: 0
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:
and here is the input form:
Another way I attempted to get the program to accept input into my char 'ClassName'
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!
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)
struct Classes { char ClassName[30]; int ClassNumber; char DaysMet[20]; int StartTime; int EndTime; char Teacher[20]; int Students; };
and here is the input form:
c++ Syntax (Toggle Plain Text)
int Input() { system("cls"); if (currentindex > IndexNum) cout << "Index is full" << endl; else while (currentindex < IndexNum) { cout << "Class Name: "; cin >> Course[currentindex].ClassName; cout << "Class Number: "; cin >> Course[currentindex].ClassNumber; cout << "Days Met: "; cin >> Course[currentindex].DaysMet; cout << "Start Time: "; cin >> Course[currentindex].StartTime; cout << "End Time: "; cin >> Course[currentindex].EndTime; cout << "Teacher: "; cin >> Course[currentindex].Teacher; cout << "Students: "; cin >> Course[currentindex].Students; currentindex++; } Menu(); return 0; }
Another way I attempted to get the program to accept input into my char 'ClassName'
c++ Syntax (Toggle Plain Text)
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!
Use std::string with getline Hope that helps
Chris
C++ Syntax (Toggle Plain Text)
#include <string> ... std::string myString; getline(std::cin, myString);
Chris
Last edited by Freaky_Chris; Jan 12th, 2009 at 6:21 pm.
Knowledge is power -- But experience is everything
•
•
Join Date: May 2008
Posts: 7
Reputation:
Solved Threads: 0
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)
Producing a result in the application looking like this:
Class Name: Class Number:
I've tried using a couple methods of input:
(not all at the same time of course)
c++ Syntax (Toggle Plain Text)
getline(std::cin, Course[currentindex].ClassName); // when the program runs, it skips this line entirely, jumping to cout << "Class Number: "; cin >> Course[currentindex].ClassNumber;
Class Name: Class Number:
c++ Syntax (Toggle Plain Text)
cin >> Course[currentindex].ClassNumber; cin.getline(Course[currentindex].ClassName, []); // Doesn't work with strings, obviously.
c++ Syntax (Toggle Plain Text)
cin >> Course[currentindex].ClassName; // works for single word answers, but goes to menu() if the input contains a space
•
•
Join Date: May 2008
Posts: 7
Reputation:
Solved Threads: 0
My header file after your recommendation:
c++ Syntax (Toggle Plain Text)
#include <string> using std::string; struct Classes { string ClassName; int ClassNumber; string DaysMet; int StartTime; int EndTime; string Teacher; int Students; };
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
Ok,this is the else part of your loop
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
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
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)
<div class="bbquote"> <div class="tlc"><div class="tli">•</div></div> <div class="blc"><div class="bli">•</div></div> <div class="trc"><div class="tri">•</div></div> <div class="brc"><div class="bri">•</div></div> <blockquote style="font-size:11px">cout << "Class Name: "; cin >> Course[currentindex].ClassName; cout << "Class Number: "; cin >> Course[currentindex].ClassNumber;</blockquote> </div>
"Civics and Economics"
there are spaces and you have got only "Civics" in your
C++ Syntax (Toggle Plain Text)
Course[currentindex].ClassName
All these things can be handled by using getline function.
The syntax of using getline with strings is
C++ Syntax (Toggle Plain Text)
getline(input_stream,name_of_string)
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)
cout << "Class Name: "; cin >> Course[currentindex].ClassName; char junk; cin.get(junk);//gets the '\n' character //even cin.ignore() works cout << "Class Number: "; cin >> Course[currentindex].ClassNumber;
Last edited by zalezog; Jan 13th, 2009 at 3:03 pm.
•
•
Join Date: May 2008
Posts: 7
Reputation:
Solved Threads: 0
Perfect! Thanks!
c++ Syntax (Toggle Plain Text)
{ system("cls"); if (currentindex > IndexNum) cout << "Index is full" << endl; else while (currentindex < IndexNum) { cout << "Class Name: "; cin.ignore(); // <---- getline(cin, Course[currentindex].ClassName); cout << "Class Number: "; cin >> Course[currentindex].ClassNumber; cout << "Days Met: "; cin.ignore(); // <---- getline(cin, Course[currentindex].DaysMet); cout << "Start Time: "; cin >> Course[currentindex].StartTime; cout << "End Time: "; cin >> Course[currentindex].EndTime; cout << "Teacher: "; cin.ignore(); // <---- getline(cin, Course[currentindex].Teacher); cout << "Students: "; cin >> Course[currentindex].Students; currentindex++; } Menu(); return 0; }
![]() |
Similar Threads
- fstream Tutorial (C++)
Other Threads in the C++ Forum
- Previous Thread: diff command (unix command) implementation in c++
- Next Thread: Simple user defined loops?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





