9 Replies - 240 Views - Last Post: 12 May 2013 - 06:51 AM Rate Topic: -----

#1 cometlove1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 21
  • Joined: 12-May 13

String help.

Posted 12 May 2013 - 06:15 AM

I posted this on another site. This is what happened.


This code string is wrong. Anyone know why?

 #include <iostream>
 #include <string>
 using namespace std;
 string mystring;
 double i;

 int main() { 

 i = 5;
 cin >> mystring;
 if (mystring == "Bye.") { 
 system("PAUSE");
 return 0; }
 if (mystring == "How are you?") {
 cout << " I'm a program, I don't have feelings."; }
 i = 5;
 cin >> mystring;
 if (mystring == "Bye.") { 
 system("PAUSE");
 return 0; }
 if (mystring == "How are you?") {
 cout << " I'm a program, I don't have feelings."; }


 }


I am using dev-c++. This supposed to be a text entry and response program. I would like the fixed code sample please to branch off of.

EditReport




May 12, 2013 at 9:00am

CroCo (82)

It is not wrong per say. The only concern I see is cin >> gets only one word until you hit space or enter. So, mystring == "How are you?" is ignored. To overcome this problem, you need to use getline().



Last edited on May 12, 2013 at 9:00am Report




May 12, 2013 at 9:05am

cometlove1 (4)

"cin.getline ()" right or is it different?

EditReport




May 12, 2013 at 9:06am

cometlove1 (4)

Also when I typed how are you the program closed even though that should be Bye being typed.

EditReport




May 12, 2013 at 9:09am

cometlove1 (4)

The call getline doesn't work when used "getline (mystring);"

This post has been edited by jimblumberg: 12 May 2013 - 06:38 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: String help.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: String help.

Posted 12 May 2013 - 06:26 AM

View Postcometlove1, on 12 May 2013 - 03:15 PM, said:

The call getline doesn't work when used "getline (mystring);"


What do you mean by "doesn't work"? Did you get an error message? If so, which one?
Was This Post Helpful? 1
  • +
  • -

#3 cometlove1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 21
  • Joined: 12-May 13

Re: String help.

Posted 12 May 2013 - 06:28 AM

10 C:\Users\Ryan Gendron\Desktop\leslie.cpp no matching function for call to `getline(std::string&)'

My brother needs the code to show off to his friends he says. I'm just trying to help.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: String help.

Posted 12 May 2013 - 06:29 AM

Oh, of course. You need to specify the stream to read from. getline(cin, mystring), not getline(mystring).
Was This Post Helpful? 0
  • +
  • -

#5 cometlove1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 21
  • Joined: 12-May 13

Re: String help.

Posted 12 May 2013 - 06:30 AM

Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 cometlove1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 21
  • Joined: 12-May 13

Re: String help.

Posted 12 May 2013 - 06:47 AM

Now this code doesn't work.

code:

#include <iostream>
#include <string>
using namespace std;
      string mystring;
      double i;
      
int main() {    
    
    i = 5;
    cout << "Hello Lauren. ";
    getline (cin, mystring);
    if (mystring == "Bye.") {    
    system("PAUSE");
    return 0; }
    if (mystring == "How are you?") {
    cout << " I'm a program, I don't have feelings."; }
    if (mystring == "Fus ro dah") {
    cout << " * Pushed back into Desktop * "; }
    if (mystring == "What's your favorite color?" {
    cout << "I have no eyes sorry."; }
}



errors:

19 expected } before { token

20 expected primary expression at end of input

20 expected ; at end of input

20 expected } at end of input

My sister wanted the code as well and her name is Lauren. Lauren is not a boy's name.

This post has been edited by jimblumberg: 12 May 2013 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: String help.

Posted 12 May 2013 - 06:47 AM

Please use code tags when posting code!

:code:

Look for missing parentheses, braces, and semicolons.


Jim

This post has been edited by jimblumberg: 12 May 2013 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#8 cometlove1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 21
  • Joined: 12-May 13

Re: String help.

Posted 12 May 2013 - 06:50 AM

Sorry.
Now this code doesn't work.

code:

#include <iostream>
#include <string>
using namespace std;
string mystring;
double i;

int main() { 

i = 5;
cout << "Hello Lauren. ";
getline (cin, mystring);
if (mystring == "Bye.") { 
system("PAUSE");
return 0; }
if (mystring == "How are you?") {
cout << " I'm a program, I don't have feelings."; }
if (mystring == "Fus ro dah") {
cout << " * Pushed back into Desktop * "; }
if (mystring == "What's your favorite color?" {
cout << "I have no eyes sorry."; }
}


errors:

19 expected } before { token

20 expected primary expression at end of input

20 expected ; at end of input

20 expected } at end of input

My sister wanted the code as well and her name is Lauren. Lauren is not a boy's name.

Code tags didn't work. Sorry.

Thanks for changing it jimblumberg.

This post has been edited by jimblumberg: 12 May 2013 - 06:50 AM
Reason for edit:: Fixed Code Tags.

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: String help.

Posted 12 May 2013 - 06:51 AM

Look for missing parentheses, braces, and semicolons.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 cometlove1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 21
  • Joined: 12-May 13

Re: String help.

Posted 12 May 2013 - 06:51 AM

Fixed it. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1