getline(cin,str) problem

Hello,
I wrote this code and I keep getting a problem in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int main()
{
	ofstream WriteToFile;
	ofstream MakeFiles;
	WriteToFile.open ("Names of courses.txt");

	int WhileVariable = 1, NumberofCourses;
	string CourseName;

	cout<<"\t\t\tElective Courses Name Entry\n\n\n";
	cout<<"Enter The Number of Elevtive Courses in The School: ";
	cin>>NumberofCourses;


	while(WhileVariable <= NumberofCourses)
	{
		cout<<"Enter Name of Course Number "<<WhileVariable<<": ";
		getline(cin,CourseName); // THE PROBLEM IS HERE <<<<<<<<<<<<<<<<<
		cout<<CourseName;
		WriteToFile << CourseName << endl;
		WhileVariable++;
		CourseName = CourseName + ".txt";
		MakeFiles.open (CourseName.c_str());
		MakeFiles<<" ";
		MakeFiles.close();
	}


	cout<<"\nYou have entered "<<NumberofCourses<<" names of courses."<<endl;

	WriteToFile.close();
	return 0;
}


After I run the program, I entered the 3 course names.

Let's say I entered 3 course names as shown below:

Advanced Physics
Electronics
Computer


What I would see in the file [Names of Courses.txt] would be this


Advanced Physics
Electronics


I want to enter names with spaces, but I get this error when i write words with 1 space.
Can someone explain to me why this happens and what I should do to fix it?

Thanks already
hannad
Last edited on
Your mixing cin >> and getline(). Don't. This will be the root cause of your problem. Your problem is actually line 17, not 23.

http://www.cplusplus.com/forum/articles/6046/
After your 'cin' in line 17 write
'cin.ignore();'
so the enter key is thrown away.
@eker676: That fails to handle the situation when someone enters something other than an integer for NumberofCourses. Not really an advisable solution.
I replied before I read your article. I know that cin will go crazy when invalid input is entered but ignore is just a quick and dirty fix that could be used. (although not a good choice)
Thank you Zaita :D
Your help is very much appreciated. Although I would like to ask you about your article.

What does this code do?
1
2
3
4
5
6
7
8
9
10
11
 while (true) {
   cout << "Please enter a valid number: ";
   getline(cin, input);

   // This code converts from string to number safely.
   stringstream myStream(input);
   if (myStream >> myNumber)
     break;
   cout << "Invalid number, please try again" << endl;
 }
 cout << "You entered: " << myNumber << endl << endl;


Why did you give a warning about an Invalid number if it was more than 0? should myStream be 0 or negative or something?

Sorry if my questions are kinda dumb, but I'm still a newbie :)

===EDIT===

I didn't see that it was >> . I thought it was >

What does >> mean exactly ? :P Thanks
Last edited on
stringstream myStream(input);
Create a stringstream with the value that the user has entered.

1
2
if (myStream >> myNumber)
     break;


Try and convert the value stored in myStream into an integer and write it to myNumber. If this is successful (e.g returns true), then we have a valid number and can break from our loop.

cout << "Invalid number, please try again" << endl;
This line will only be executed if the above if-statement returns false. Indicating an invalid number.

>> really means write from >> to.
So when you see cin >> var; that means write a value from cin TO var.
Last edited on
Thanks a lot :D

==EDIT==

I tried adding your code but now i have to enter the number twice and the same error that happened earlier is happening now. This is the code. I think I did something wrong!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>
using namespace std;
int main()
{
	ofstream Write_To_File;
	ofstream Make_Files;
	Write_To_File.open ("Names of courses.txt");

	int While_Variable = 1, Number_of_Courses;
	string Course_Name, str_Number_of_Courses;

	cout<<"\t\t\tElective Courses Name Entry\n\n\n";
	while (true)
	{
		cout<<"Enter The Number of Elevtive Courses in The School: ";
		getline(cin, str_Number_of_Courses);
		stringstream myStream(str_Number_of_Courses);
		if (myStream >> Number_of_Courses)
			break;
		cout << "Invalid number, please try again" << endl;
	}

	while(While_Variable <= Number_of_Courses)
	{
		cout<<"Enter Name of Course Number "<<While_Variable<<": ";
		getline(cin,Course_Name);
		cout<<Course_Name;
		Write_To_File << Course_Name << endl;
		While_Variable++;
		Course_Name = Course_Name + ".txt"; 
		Make_Files.open (Course_Name.c_str());
		Make_Files<<" ";
		Make_Files.close();
	}


	cout<<"\nYou have entered "<<Number_of_Courses<<" names of courses."<<endl;

	Write_To_File.close();
	return 0;
}
Last edited on
Works fine for me. As expected.
I am using Microsoft Visual C++ 6.0. My code gives me the following:

Enter The Number of Elevtive Courses in The School: 2
2

Enter Course Number 1: First
2Enter Course Number 2: Course
Course
You have entered 2 names of courses.

IN THE FILE
2
First


Nevermind :P. I solved it by entering _ instead of a space :)
Last edited on
Topic archived. No new replies allowed.