If statements need help please

This is a discussion on If statements need help please within the C++ Programming forums, part of the General Programming Boards category; How do I get a if statement to work on C++ I want it so if you said no on ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    If statements need help please

    How do I get a if statement to work on C++
    I want it so if you said no on a message like this

    #include <iostream>
    #include <string>
    using namespace std;

    int main ()
    {
    string are

    cout << "Are you ok? :";
    cin >> are;

    this one would be for if they said yes
    cout << "Wells thats good " << endl;


    this one would be for if they said No
    cout << "thats Terrable" << endl;


    does anyone know what I mean Please help

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    48
    I think this is what you mean:


    #include <iostream.h>

    // for getchar()
    #include <stdio.h>
    #include <stdlib.h>

    int main() {
    char answ[10]; // character array

    cout << "Are you ok?: ";

    cin << answ;

    if (answ[0] == 'y') // check if first leter is y
    cout << "Wells thats good " << endl;

    else if (answ[0] == 'n') // check if first leter is n
    cout << "Thats terrible " << endl;

    getchar(); // pause program before end
    }

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4

    ok

    I never done anything like this but I fixed a few errors but I dont know what the other 7 are

    #include <stdio.h>
    #include <stdlib.h>
    int main()

    {
    char answ[10]; // character array

    cout << "Are you ok? :";
    cin >> answ;

    if (answ[0] == 'y') // check if first leter is y
    cout << "Wells thats good" << endl;

    else
    if (answ[0] == 'n') // check if first leter is n
    cout << "Thats terrible" << endl;

    getchar(); // pause program before end
    return 0;
    }



    there are 7 errors left whats wrong?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    codes tags!!!

    and you didnt include iostream when you went to run your program king

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    int main() 
    {
    	char answ[10]; // character array
    
    	cout << "Are you ok? :";
    	cin >> answ;
    
    	if (answ[0] == 'y') // check if first leter is y
    		cout << "Wells thats good" << endl;
    
    	else if (answ[0] == 'n') // check if first leter is n
    		cout << "Thats terrible" << endl;
    
    	getchar(); // pause program before end 
    
    	return 0;
    }

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by Jamsan
    codes tags!!!

    and you didnt include iostream when you went to run your program king

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    int main() 
    {
    	char answ[10]; // character array
    
    	cout << "Are you ok? :";
    	cin >> answ;
    
    	if (answ[0] == 'y') // check if first leter is y
    		cout << "Wells thats good" << endl;
    
    	else if (answ[0] == 'n') // check if first leter is n
    		cout << "Thats terrible" << endl;
    
    	getchar(); // pause program before end 
    
    	return 0;
    }

    Bad...

    Don't mix output streams. It can lead to some very freaky behaviour. Instead of using getchar (a C-style i/o function), use cin.get().

    BTW, once you do this, stdlib and stdio aren't needed. (stdlib never was needed)

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Don't use <iostream.h> either. It's outdated. Use <iostream> and using namespace std; instead.

    Also, use <cstdlib> and <cstdio>
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by XSquared
    Don't use <iostream.h> either. It's outdated. Use <iostream> and using namespace std; instead.
    those still aren't taught in alot of tutorials and programming classes... i've really only seen iostream a rare few times...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    the tutorials and books are outdated then. standard calls for the use of iostream without the .h

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    4

    alright

    Alright cool its works now..I added some stuff No errors at all

    but now uhhh how do I fix this when I say yes or no when I get in the program from the exe file when I say yes it will just exit along with saying no.

    #include <cstdlib>
    #include <iostream>
    #include <cstdio>
    using namespace std;

    int main()
    {
    char answ[10]; // character array

    cout << "Are you ok? :";
    cin >> answ;

    if (answ[0] == 'y') // check if first leter is y
    cout << "Wells thats good" << endl;

    else if (answ[0] == 'n') // check if first leter is n
    cout << "Thats terrible" << endl;

    getchar(); // pause program before end

    return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    4

    like

    like how would i keep going further then besides haveing just one yes or no statement like if they hit yes it will ask them another question and they would answer again

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    copy the code...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What was wrong with the original approach?
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string are;
    
      cout << "Are you ok? :";
      cin >> are;
    
      if(are == "yes")
        cout << "Wells thats good " << endl;
      else
        cout << "thats Terrable" << endl;
    
      return 0;
    }
    Unlike the char[10] solutions, this one WON'T overflow.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    now that i look at it there is no problem with that code... none that i can see... i just assumed there was a problem... oh well...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The only problem I see is a misspelling of 'terrible'.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 11:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 12:36 PM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21