uhhh strings and cin problems

Mar 3, 2013 at 3:51pm
uhhh strings and cin problems

In my attempts to figure out a way to store a sentence into a string variable I have come up with nothing. Either it stop at the first word or add the first word to the end.

ok... so I decided to try to find a way around it.

My goal is to compare the two sentences with each other.

ie

foo bar ar
compared to
foo bar ar

simple right...I guess for a beginner that is not so.

here is code I have tried

1
2
string firstTry;
cin >> firstTry;


What I got back was:

foo

I need the rest of the sentence...back to the drawing board.

1
2
3
4
5
string secondTryA;
string secondTryB;
string secondTryC;

cin >> secondTryA, secondTryB, secondTryC;


results:
foo bar arfoo

Ok the first time it didn't give enough and now it's giving to much... back to the drawing board.

1
2
char thirdTry[10];
cin thirdTry;


results:
foo bar arfoo

I am trying, really I am, but really this is c++. Storing an user input string in a variable should not be that hard.

I tried getline, but I couldn't get it to store in the variable. I thought maybe concatation is the way to go. I'll have to read up on it, and don't know if that will be another dead end.

Any suggestions....

Is C++ more for numbers, ints, and bools then for working with words?

Why is it adding the first word to the end?

How can I get it to leave the first word off the end to accurately compare it?

I've been at this all night and I would hate it all to be for nothing...help???
Mar 3, 2013 at 3:53pm
Use function

std::getline( std::cin, SomeStringVariable );
Mar 3, 2013 at 3:54pm
What problems did you have with getline(), that is one of the preferred methods of retrieving a line of text in C++? See this link: http://www.cplusplus.com/reference/string/string/getline/



Mar 3, 2013 at 6:02pm
I will try it again because I've tried so many I can't remember what the problem was.

I think I did something like....

1
2
string someString;
getline(cin, someString);


It didn't work.

I am going to try again now. But I do have a question.

The reason I didn't put use std:: in front of getline is that I thought
using namespace std; meant you didn't have to use std::.

Is it just a form of style?

Do you mix code with std:: with code without it while using namespace std?

I don't quite understand why some use it and some don't??
Mar 3, 2013 at 6:15pm
Use the getline function. This will get all the chars.

E.g.

std::string Str;
getline(cin, Str);

you can use using namespace std; but some compilers get confused if you dont
use std::
Mar 3, 2013 at 6:15pm
Here's some info on using namespace std;.
http://www.parashift.com/c++-faq/using-namespace-std.html

As for the comparison of two strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
	std::string input_1;
	std::string input_2;

	std::cout << "Enter string one: ";

	// Don't have to use '\n', but I always use it,
	// even though it's the default.
	std::getline( std::cin, input_1, '\n' );

	std::cout << "Enter string two: ";
	std::getline( std::cin, input_2, '\n' );

	if( input_1 == input_2 )
		std::cout << "Same input...\n";
	else
		std::cout << "Difference in inputs...\n";

	return 0;
}
Last edited on Mar 3, 2013 at 6:16pm
Mar 4, 2013 at 6:01am
Thanks everyone you really cleared up a lot for me. The std:: thing is something that I really didn't understand. Now, I know.

@ Lynx876 your example was awesome. I didn't quite do it the way you did and I know that is the reason it didn't work. I followed your example in my code and couldn't believe when the results popped up correctly. In less then a second wow!!!

@ jlb I think getline is now my preferred method too.

kudoos to you guys and the speedy reply.
Topic archived. No new replies allowed.