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:

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:

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'

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

#include <string>
...
std::string myString;
getline(std::cin, myString);

Hope that helps

Chris

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)

getline(std::cin, Course[currentindex].ClassName);  // when the program runs, it skips this line entirely, jumping to 
		cout << "Class Number: ";
		cin >> Course[currentindex].ClassNumber;

Producing a result in the application looking like this:

Class Name: Class Number:

cin >> Course[currentindex].ClassNumber;
		cin.getline(Course[currentindex].ClassName, []);  // Doesn't work with strings, obviously.
cin >> Course[currentindex].ClassName;  // works for single word answers, but goes to menu() if the input contains a space

My header file after your recommendation:

#include <string>
using std::string;

struct Classes
{
	string ClassName;
	int	ClassNumber;
	string DaysMet;
	int StartTime;
	int	EndTime;
	string Teacher;
	int Students;

};

Ok,this is the else part of your loop

cout << "Class Name: ";
cin >> Course[currentindex].ClassName;
cout << "Class Number: ";     
cin >> Course[currentindex].ClassNumber;

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

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

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

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;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

//even cin.ignore() works

Perfect! Thanks!

{	
	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;
}

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


Chris

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?

This question has already been answered. Start a new discussion instead.