getline problem

Heyre people, i have a little problem.
I am learning C++ in order to develop my own software, so i am following some documents and tutorials, and i have reached the cin and getline part, and i have followed kindly the tutorial, but have some problem...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int age;
	cout << newline << "Please Input your age!" << newline;
	cin >> age;
	cout << "We have registered that you are " << age << " years old" <<newline;
	Sleep(1000);
	string names;
	cout << "What's your name? " << newline;
	getline (cin, names);
	Sleep(500);
	cout << "Nice name " << names << "." <<newline;
	string mystra;
	cout << "What might your favourite team be? " << newline;
	getline (cin, mystra);
	Sleep(500);
	cout << "I am also a fan of " << mystra << " too! nice :)" <<newline;
	Sleep(1000);

So, the cin age works, it works perfectly,
but however, the first getline does not wait for my output, it imediatelly executes the following code, and say's "Nice Name ."
and asks what my favourite team is, but there getline works, however if i replac the first getline by cin >> names, however that only works for the first word, the team's getline starts acting as the names getline used to act..
ANy help please :) ?
thanks!
add cin.ignore(80, '\n'); after cin >> age;

This will clear the input buffer of any excess junk that we can't use and prevents the names from being stored as a new line character. The example isn't wrong, it just doesn't work properly on Window's systems.
This is really not trouble, if you declare an array "a", you can use directly "cin > > a", and it has the same effect of getline function.
But a string isn't treated as an array, it's treated as a string. If you try to cin a string, you'll only get the string you typed before you enter a space. This is a known delimiter for cin. The workarounds are to use getline or something else that has a modifiable delimiter (getline has a default delimiter of '\n').

Technically, trying to cin an array without specifying an index will throw a compiler error:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main() {
   std::string myStringArray[5];
   std::cout << "Enter 5 Strings: ";
   std::cin >> myStringArray;

   for (int i = 0; i < 5; i ++)
      std::cout << myStringArray[i] << " ";
   return 0;
}


Code Blocks Compiler wrote:
C:\Programming\Test Project\main.cpp|7|error: no match for 'operator>>' in 'std::cin >> myStringArray'|


Edit: Modifying the code above to actually run, you get this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main() {
   std::string myStringArray[5];
   std::cout << "Please enter 5 strings:\n";
   for (int i = 0; i < 5; i ++)
      std::cin >> myStringArray[i];

   for (int i = 0; i < 5; i ++)
      std::cout << "\nString " << i + 1
                     << ": " << myStringArray[i];
   return 0;
}
Please enter 5 strings:
Hello World
This is my program

String 1: Hello
String 2: World
String 3: This
String 4: is
String 5: my
Last edited on
Thanks everyone for the replies.
i have not studied what arrays are, i am very green, for now, in what comes to programming. I am studying the documentation, but before i proceed, id like to clear this getline and cin problem.
i have tried as sugested above cin.ignore(80, '\n'), it worked, but created another problem. If yo notice, on the cout i used << newline as i had defined newline as \n, just to clear any doubts there.
The problem now is, it asks my name, i type it, press enter, nothing happens, just creates a new line, but if i press enter again, it works. same happens with the favourite team, although the age part, with the cin, even with the cin.ignore, works perfectly. Id like to sort this out before i do proceed, then i'l learn arrays and those std:: thingies :b
Thanks in advance!
Anyways, on a side note, besides my problem, can you explain that for (int i = 0; i < 5; i ++) thing? that is a bit confusing to me. But hell, il get over my problems, i will sucessfully learn this knowledge, mark my words, i know a tad of javascript and html, not much, but i want to code software, but all in all, i am quite green in what comes to scripting.
You don't need ignore() after the getline()s, only after a >>.

>> extracts and ignores all white space (and newline) in the buffer, then reads your data, and then stops. Any white space that was left in the buffer (including the new line you made when you hit enter) is not extracted.

There is a third argument in getline(), and that is a delimiting character. By default, it is '\n'. Basically the function says "read from cin into names until you reach a new line".

The >> left a new lline, so that's why the getline() didn't work.

As for the arrays and for loops, You'll get there.
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
#include <iostream>
#include <string>
#include <Windows.h>
#include <ctime>
#include "conio.h"
using namespace std;

void age()
{
	int age;
	string names, mystra;

	cout << "Please Input your age!" << endl;
	cin >> age;
	cin.ignore(80, '\n'); 
	cout << "We have registered that you are " << age << " years old" << endl;
	Sleep(1000);
	cout << "What's your name? " << endl;
	getline(cin,names);
	Sleep(500);
	cout << "Nice name " << names << "." << endl;
	cout << "What might your favourite team be? " << endl;
	getline(cin,mystra);
	Sleep(500);
	cout << "I am also a fan of " << mystra << " too! nice :)" << endl;
	Sleep(1000);
}

int main()
{
	age();
}


next time past ur whole code.........
Thanks, it works great.
I did not add the conio.h nor the ctime includes. i already had the previous ones, what is the effect of the 2 above mentioned?
Thanks in advance, once again!
ctime is not needed in your program, but it is a header file that uses the standard time functions. conio.h is an old Microsoft library that is no longer supported. It's not standard and therefore doesn't do the same on every system. I suggest not worrying about the conio.h, and the ctime header is needed for randomization.

I assume they just use some default header classes in their programs and are always included.
Topic archived. No new replies allowed.