Thread: Super-New Question

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    5

    Super-New Question

    Hi, first time attempting this. I have been web programming with old school HTML for a number of years as a hobby - and have exhausted its limitations a long time ago. Currently I have spent about... 3 days trying to learn some C++ - this is what I got down so far (very limited - be gentle).

    The program below is not meant to be of any use - just review of what I've learned. What I wanted it to do is basically loop the function that asks "please report a small number" (if the answer is no/FALSE - I want to continue to loop until the answer is TRUE) - however I am to understand that the compiler and the program read code from top to bottom - how do you get it to go back up to read a var/fucntion that has changed?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        int numb001;
        string myname;
    
            
        cout<<"Hi, I will be summarizing my skills to date; this program \nwas written by Nacent_Grammer.";
        cout<<"\n---------------------------------------------------------";
        cout<<"\n\nPlease enter your name first: ";
        cin>> myname;
        cin.ignore();
        
        cout<<"\nHello " << myname <<".  Glad you could join me today!  Let us begin \n";
        
        cout<<"\nFirst " <<myname <<", please report a small number to me: \n\nNumber: ";
        cin>> numb001;
        cin.ignore();
        
        cout<<"\nThe number you entered was: '" << numb001 <<"'.  Is this correct?\n\n 1 = Yes | 2 = No";
        
        int confirm001; // Confirmation of either 1 = Yes or 2 = No!
        
        cout<<"\n\nSelection: ";
        
        cin>> confirm001; // First confirmation.
        cin.ignore();
        if ( confirm001 == 1 ) { // if YES
             cout<<"\nOk, time to move on...";
             system ("PAUSE");
             }
        else if ( confirm001 == 2 ) {  // if NO
             cout<<"\nOK, what is it then: ";
             cin>> numb001;
             cin.ignore();
             cout<<"\n\nIs this correct now?";
             cout<<"\n\nSelection: ";
             confirm001 != 1 , 2;
             cin>> confirm001;
            }
        
        cin.get();
    
    /* The beginning of the variable list - includes menu selections and variables to be computed*/    
       
        float sum01;
        float nsum; // end of sum
        float mult01;
        float nmult; // end of multiplication
        float divide01;
        float ndivide; // end of divide
        float subtract01;
        float nsubtract; // end of substraction
        
        // End of Menu/Variable options   
        
        int selection001; // menu selection variable!
        
        cout<<"\n\n---------------------------------------------------------";
        cout<<"\n\nOk, now to move on.  You said your number was '" <<numb001 <<"'.  \nI would like to do some math for you!";
        cout<<"\n\n  Please, select from the following: \n\n    (1)  Add another number to your old number \n    (2)  Multiply your old number with a new number \n    (3)  Divide some numbers!\n    (4)  Or, subtract some numbers!\n\n";
        cout<<"\n\nSelection: ";
        
    // Beginning of IF statements for menu selection!
        
        cin>> selection001;
        if ( selection001 == 1 ) {
             cout<<"\n\nWhich number do you wish to add then? \n\nNew number: ";
             cin>> nsum;
             cin.ignore();
             sum01 = numb001 + nsum;
             
             cout<<"\n\nWhen you add '" << numb001 <<"' and '" << nsum <<"' you get '" << sum01 <<"'\n\nThank you, I must close the program now!";
             }
             
        else if ( selection001 == 2 ) {
             cout<<"\n\nWhich mumber do you wish to multiple then? \n\nNew number: ";
             cin>> nmult;
             cin.ignore();
             mult01 = numb001 * nmult;
             
             cout<<"\n\nWhen you multiply '" << numb001 << "' and '" << nmult <<"' you get '" << mult01 <<"'\n\nThank you, I must close the program now!";
             }
        else if ( selection001 == 3 ) {
             cout<<"\n\nWhich number do you wish to divide then? \n\nNew number: ";
             cin>> ndivide;
             cin.ignore();
             divide01 = numb001 / ndivide;
            
             cout<<"\n\nWhen you divide '" << ndivide <<"' into '" << numb001 <<"' you get '" << divide01 <<"'\n\nThank you, I must close the program now!";
             }
        else if ( selection001 == 4 ) {
             cout<<"\n\nWhich new number do you wish to subtract from your old number, '" << numb001 <<"'?\n\nNew number: ";
             cin>> nsubtract;
             cin.ignore();
             subtract01 = numb001 - nsubtract;
             
             cout<<"\n\nWhen you subtract '" << nsubtract <<"' from '" << numb001 <<"' you get '" << subtract01 <<"'\n\nThank you, I must close the program now!";
             }
    // End of IF statements!
    
        cin.get();
    }
    I have been going through the tutorials and I have been trying to read other postings (even got myself a little manual about 500 pages long), but I think I am asking a question a little a head of myself.

    -J
    Last edited by Nascent_Grammer; 04-07-2007 at 11:28 PM.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Also, I am aware that I should not post my ENTIRE program, however, my hope is that someone might take the time to review what I've written (it is not much) and perhaps give me some pointers?

    There is a line of code that says "confirm001 != 1, 2;" that was me trying to reset the variable so I could get it to loop - but, I dunno - Im lost.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Quote Originally Posted by Nascent_Grammer
    There is a line of code that says "confirm001 != 1, 2;" that was me trying to reset the variable so I could get it to loop - but, I dunno - Im lost.
    Really? I don't know exactly what it will do if you set confirm001!=1,2. I imagine it change to some weird number like the kind that show up when you don't give your variables a value. For the sake of clarity why not just reset confirm001=0?

    Looping the program would involve something like this:
    Code:
    confirm001=0;
    while(confirm001!=1){       //loop while cofirm001 isn't equal to 1
    cin>>confirm001;
    if (confirm001==2){
    cout<<"\nOK, what is it then: "; //and whatever else you want here im not going to paste it
    }
    else if(confirm001==1){
    cout<<"\nOk, time to move on...";
    return 0;   /*this line will end the program. remove this line and the program will not exit it will just continue on with the program having exited the loop*/
    }
    }
    It's usually a good idea to add return 0; at the end of your program right after that cin.get(); Tutorial on loops.
    Last edited by cerin; 04-08-2007 at 01:28 AM.
    My computer is awesome.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Here's a tip:
    Make sure that the very next thing you learn is the "switch" statement. It should actually be used in place of all of the if statements in that program.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    I have read the tutorial on switches, but I dont see why..?

    What I mean to say is that if "If statements" get the job done - why use switches? I am seeing only the redundancy and not the value (I assume I am not seeing why) - can you briefly explain maybe in other words not in the tutorial why a switch would be used in /my/ program rather than If statements?

    -Thanks,

    -Justin

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Nascent_Grammer View Post
    can you briefly explain maybe in other words not in the tutorial why a switch would be used in /my/ program rather than If statements?
    For quite a few reasons:

    • Switch statements are easier to read when you have a load of conditions to run through on a single variable.
    • Switch statements can take advantage of the fallthrough from one case to another when needed.
    • This may not apply currently, but I've read that switch statements are easily recognized by compilers as easy to translate to faster assembly code than when given the equivalent in if statements. Modern compilers might be just as quick when handling if statements. Someone else can probably clarify this point for me.


    Just because something "gets the job done" doesn't mean that you should use it. If we used that logic, we would be using goto statements all the time, wouldn't we?

  7. #7
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by cerin View Post
    if you set confirm001!=1,2. I imagine it change to some weird number like the kind that show up when you don't give your variables a value.
    Of course not! Nothing will happen at all.
    The operator != does not modify its first or second operand. It evaluates them and returns a bool. So, in C++ (1,2) would evaluate to 2, but without the (), the whole statement evaluates as follows:

    Code:
    confirm001!=1,2;
    ->
    Code:
    operator!= (confirm001, 1);
    2;
    ->
    Code:
    false;
    2;
    which is allowed but does nothing at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. With super powers would you:
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-17-2003, 11:27 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, 01:47 AM