new question :P

This is a discussion on new question :P within the C++ Programming forums, part of the General Programming Boards category; Code: // if_statements // Program uses an if statement to have the user choose a specific selection #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    new question :P

    Code:
    // if_statements
    // Program uses an if statement to have the user choose a specific selection
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int selection;
        int storage[3];
    
        storage[0] = 1;
        storage[1] = 2;
        storage[2] = 3;
    
        /*Opening Statement*/
        std::cout << "This program utilizes an if statement to select a number"; cin.get();
    
        std::cout << "Please select the number 1" << endl;
        istream &get (int& selection);
    
        if (selection = storage[0])
            std::cout << "Good job smartass, you can read";
        else
        {
            std::cout << "Holy ........, your really stupid, arn't you? you cant even take simple directions? go away."
                      << endl << "Error 01: Your to stupid";
            return 0;}
    
    
    return 0;
    }
    My problem is that
    Code:
    istream &get (int& selection);
    Isnt working... am I using it wrong? im trying to get the program to take in an answer and store it in int selection...
    Last edited by newbreed; 04-29-2007 at 01:49 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    why not just use "cin >> variableName" ?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To get input you'd normally do:
    Code:
    cin >> variable;
    What you are doing is declaring a function.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    wow... im full of questions today...

    the program keeps telling me im right even when im wrong...

    Code:
    // if_statements
    // Program uses an if statement to have the user choose a specific selection
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int selection;
        int storage[3];
    
        storage[0] = 1;
        storage[1] = 2;
        storage[2] = 3;
    
        /*Opening Statement*/
        std::cout << "This program utilizes an if statement to select a number"; cin.get();
    
        std::cout << "Please select the number 1" << endl;
        cin >> selection;
    
        if (selection = storage [0])
            std::cout << "Good job smartass, you can read";
        else
        {
            std::cout << "Holy ........, your really stupid, arn't you? you cant even take simple directions? go away."
                      << endl << "Error 01: Your to stupid";
            return 0;};
    
    
    return 0;
    }
    Last edited by newbreed; 04-29-2007 at 02:10 PM.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if (selection = storage [0])
    So?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    so? when I type a wrong answer it gives me the answer for being right
    Last edited by newbreed; 04-29-2007 at 02:26 PM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what anon is saying is that
    Code:
    if (selection = storage [0])
    isnt doing what you think its doing. there is a difference between '=' and '=='. when testing a value for equality, use double equals. single equals is to set a value.

    also, since you have 'using namespace std;', you can remove all the 'std::'s

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    also, since you have 'using namespace std;', you can remove all the 'std::'s
    Or rather, remove "using namespace std;" because you've picked up a good habit with std::

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes your right it would be good practice for when you use different namespaces before. ive never had to, and it wouldnt be difficult to start adding std:: in front. i think it just looks cleaner and easier to read to just have the using statement, if you know your only going to need std.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    I removed it and it took away my abilities to use "cin" and "endl"...

    Code:
    // if_statements
    // Program uses an if statement to have the user choose a specific selection
    
    #include <iostream>
    #include <fstream>
    //using namespace std;
    int main()
    {
        int selection;
        int storage;
    
        storage = 1;
    
    	/*Opening Statement*/
    	std::cout << "This program utilizes an if statement to select a number"; cin.get();
    
    	std::cout << "Please select the number 1" << endl;
        cin >> selection;
    
    	if (selection == storage)
            std::cout << "Good job smartass, you can read";
        else
        {
            std::cout << "Holy ........, your really stupid, arn't you? you cant even ta"
            << "ke simple directions? go away."
                      << endl << "Error 01: Your to stupid";
            return 0;};
    
    
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    you have to add std:: before endl and cin too

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    ah, thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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