Help with c++ please :)

General technological topics without their own forum go here

Help with c++ please :)

Post by cyber-maniac on Thu Jun 23, 2011 12:41 pm
([msg=58901]see Help with c++ please :)[/msg])

Code: Select all
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

char x;

int main()
{
    cout << "Is Family Guy funny?" << endl;
    cin >> x;
    if (x == 'Yes' || x == 'yes' )
          cout << "Correct!" << endl;
    else
        cout << "WRONG" << endl;
       
        system("Pause");
        return 0;
           
    }



I need help with this code, im just experimenting.

The issue is that when i run the code and type yes or Yes it still returns "WRONG" instead of "Correct!", can anyone help?
cyber-maniac
Poster
Poster
 
Posts: 113
Joined: Tue Feb 22, 2011 7:13 am
Blog: View Blog (0)


Re: Help with c++ please :)

Post by LoGiCaL__ on Thu Jun 23, 2011 1:10 pm
([msg=58904]see Re: Help with c++ please :)[/msg])

Try making:

Code: Select all
if (x == 'Yes' || x == 'yes' )


like this:

Code: Select all
if((x == "Yes") || (x== "yes"))


also

Code: Select all
char x;


That will only hold one character. You are trying to fit Yes or yes into it.

Try:

Code: Select all

  string x;



Don't for get to put double quotes around "Yes" and "yes". That should give an error depending on what you are using to compile it. It is probably evaluating Y || y to Yes || yes .
User avatar
LoGiCaL__
Moderator
Moderator
 
Posts: 1047
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Help with c++ please :)

Post by Acidiferous on Thu Jun 23, 2011 3:12 pm
([msg=58905]see Re: Help with c++ please :)[/msg])

Hey,

A few details would be using cin.get() instead of system("pause") , it's more standard c++. Also you include cmath, but you dont really use it?

Code: Select all
#include <iostream>
#include <string>

using namespace std;

string x;

int main() {
   
    cout << "Is Family Guy funny?" << endl;
    cin >> x;
    if ((x == "Yes") || (x == "yes")) {
          cout << "Correct!" << endl;
    }
    else {
        cout << "WRONG" << endl;
    }

    cin.ignore(); // this will ignore the '/n' the user ends the input with
    cin.get();
    return 0;
}


But with LoGiCaL__'s changes it should run fine :)
Acidiferous
Experienced User
Experienced User
 
Posts: 55
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: Help with c++ please :)

Post by cyber-maniac on Fri Jun 24, 2011 11:55 am
([msg=58952]see Re: Help with c++ please :)[/msg])

You mentioned cin.get() what does it do?

Also the cmath was from a previous experimental script i was testing, so i just left it.
cyber-maniac
Poster
Poster
 
Posts: 113
Joined: Tue Feb 22, 2011 7:13 am
Blog: View Blog (0)


Re: Help with c++ please :)

Post by Acidiferous on Fri Jun 24, 2011 12:47 pm
([msg=58956]see Re: Help with c++ please :)[/msg])

The quick answer is here: http://lmgtfy.com/?q=cin.get()&l=1 :)

But really it just sits there, waiting for some input. Like the system("pause") or getch() , but instead of just working on one platform, it can be compiled and executed on multiple.
Acidiferous
Experienced User
Experienced User
 
Posts: 55
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: Help with c++ please :)

Post by cyber-maniac on Sat Jun 25, 2011 4:09 am
([msg=58989]see Re: Help with c++ please :)[/msg])

How can i input 2 words with the same string?
cyber-maniac
Poster
Poster
 
Posts: 113
Joined: Tue Feb 22, 2011 7:13 am
Blog: View Blog (0)


Re: Help with c++ please :)

Post by Acidiferous on Sat Jun 25, 2011 1:58 pm
([msg=59005]see Re: Help with c++ please :)[/msg])

I'm not sure i know what you mean. Can you give an example?
Acidiferous
Experienced User
Experienced User
 
Posts: 55
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: Help with c++ please :)

Post by LoGiCaL__ on Sat Jun 25, 2011 2:15 pm
([msg=59008]see Re: Help with c++ please :)[/msg])

How can i input 2 words with the same string?


Instead of using:

Code: Select all
cout << "Enter two words: ";
cin >> twoWordVar;


use:

Code: Select all
cout << "Enter two words:";
getline(cin, twoWordVar);
cout << twoWordVar << endl;


Code: Select all
cin >>


Will only take the string up to the first space then cut the rest off.
User avatar
LoGiCaL__
Moderator
Moderator
 
Posts: 1047
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Help with c++ please :)

Post by cyber-maniac on Sun Jun 26, 2011 3:54 am
([msg=59046]see Re: Help with c++ please :)[/msg])

what i tried was:

Code: Select all

int main()

string x;
string y;

cout << "enter your name" << endl;
cin >> x;
cin >> y;
cout << x+y << endl;


cyber-maniac
Poster
Poster
 
Posts: 113
Joined: Tue Feb 22, 2011 7:13 am
Blog: View Blog (0)



Return to General

Who is online

Users browsing this forum: No registered users and 0 guests