Thread: plzzz!!! answer this c++proggram on how to use do,while code?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Unhappy plzzz!!! answer this c++proggram on how to use do,while code?




    this is my code so far!!!,,
    where do i put do and while??



    #include<iostream>
    #include<string>
    using namespace std;
    void main()
    {
    string password;
    float sum,difference,product,qoutient,num1,num2;
    char mychoice;
    cout<<"enter your password"<<endl;
    cin>>password;
    if(password=="frozen")
    {
    cout<<"select your choice:"<<endl;
    cout<<"select a for addition:"<<endl;
    cout<<"select s for subtraction:"<<endl;
    cout<<"select m for multiplication:"<<endl;
    cout<<"select d for division:"<<endl;
    cin>>mychoice;
    cout<<"enter do you want to continue"<<endl;
    switch(mychoice)
    {
    case'a':
    cout<<"enter first number:"<<endl;
    cin>>num1;
    cout<<"enter second number:"<<endl;
    cin>>num2;
    sum=num1+num2;
    cout<<"the sum is:"<<sum<<endl;
    break;
    case's':
    cout<<"enter first number:"<<endl;
    cin>>num1;
    cout<<"enter second number:"<<endl;
    cin>>num2;
    difference=num1-num2;
    cout<<"the diffrence is:"<<difference<<endl;
    break;
    case'm':
    cout<<"enter first number:"<<endl;
    cin>>num1;
    cout<<"enter second number:"<<endl;
    cin>>num2;
    product=num1*num2;
    cout<<"the product is:"<<product<<endl;
    break;
    case'd':
    cout<<"enter first number:"<<endl;
    cin>>num1;
    cout<<"enter second number:"<<endl;
    cin>>num2;
    qoutient=num1/num2;
    cout<<"the qoutient is:"<<qoutient<<endl;
    break;
    default:
    cout<<"invalid choice"<<endl;
    }
    }
    else
    cout<<"access denied"<<endl;
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    First learn to indent properly.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Put it where ever you feel it needs to be. Also, use code tags and indentation, because no one can read it (it's like reading english letters upsidedown and sideways at random).
    In general, you put the do at the start of what you want to loop, and the while and statement at the end. Have a go and then ask.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    thank a lot,,, i try if i can do that,,take care always, godbless^_^

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    First learn to indent properly.
    It doesn't help that some instructors don't bother to even mention indentation, much less talk about doing it properly.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Please do not post baby code. Please make your code systatic indent.I will help you.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Post

    do is for looping and while is for condition to stop and show any action .
    Example :

    Code:
    do {
         x=x+1;
         // It loops continuous until it stop through while condition.
    }
    while(x>=5);{
       cout<< "you have reach on 5";
    }

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Smile

    hi,,,goodnoon to you sir, thank for giving me an example on how to use code do,while,,
    will you plzzz use do,while code to my input code??? if its ok to you? hope you will do that?
    im sorry if i cant speak english very well,, because im just a filipino,,, dont know much english,,,

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Black Diamond View Post
    do is for looping and while is for condition to stop and show any action .
    Example :

    Code:
    do {
         x=x+1;
         // It loops continuous until it stop through while condition.
    }
    while(x>=5);{
       cout<< "you have reach on 5";
    }
    your indentation suxxes big deal

    Code:
    do {
         x=x+1;
         // This loop continuous while condition is true.
    } while(x>=5);
    
    cout<< "x value has exceeded 5";
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    I hate to be the one to drop the rules bomb but:

    http://cboard.cprogramming.com/showthread.php?t=13473

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    What they mean by indentation is that your code is broken up nicely so that it is easier to read.Like below its easier to see when varibles are being initialised and so on

    Code:
    #include<iostream>
    #include<string>
    
    int main(){
    
    int someVar=0;
    int anotherVar=1;
    int userInput;
    
    switch(userInput){
                         case 1:
                                    cout<<userInput<<endl;
                                    break;
    
                          case 2:
                                     cout<<userInput<<endl;
                                     break;
          }
    return 0;
    }
    I have only started out programming but if your code is presented so that other people can read it easily the programmers on this site are quick to answer either telling you where you went wrong or at least point you in the right direction.

    also where varibles have 2 (or more)words,capitalise the 2nd words first letter,it makes it the varible name standout more

  12. #12
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    This is how I would of did it

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    double addition ( double x, double y );
    double subtraction ( double x, double y );
    double multiplication ( double x, double y );
    double division ( double x, double y );
    
    int main ( ) 
    {
        string thePassWord = "frozon";
        string changePassWord = "fire";
        string passWord;
        char myChoice;
        double x, y;
        int count = 0;
        
                   
        do {
            
             cout << "Enter password: \n>";
             getline ( cin, passWord, '\n' );
             
             count++;
             
             if ( count == 3 ) 
             {
                  cout << "Error: You enter the password wrong 3 time. \n";
                  cout << "Password is being changes. \n";
                  
                  thePassWord.swap (changePassWord);
                  
             }
             
            } while ( passWord != thePassWord );
                    
        if ( passWord == thePassWord ) 
        {
             while ( true ) 
             {
                 cout << "Select your choice: \n";
                 cout << "Select \'a\' for addition: \n";
                 cout << "Select \'s\' for subtraction: \n";
                 cout << "Select \'m\' for multiplication: \n";
                 cout << "Select \'d\' for division: \n>";
                 
                 cin >> myChoice;
                 
                 switch ( myChoice ) 
                 {
                        case 'a':
                        case 'A':
                             
                             cout << "Addition: \n";
                             cout << "First number: \n>";
                             cin >> x;
                             cout << "Second number: \n>";
                             cin >> y;
                             
                             cout << endl;
                                        
                             cout << x << " + " << y << " = " << addition ( x, y ) << "\n\n";
                             
                             cin.get();     
                             break;
                         
                        case 's':
                        case 'S':
                             
                             cout << "Subraction: \n";
                             cout << "Frist number: \n>";
                             cin >> x;
                             cout << "Second number: \n>";
                             cin >> y;
                             
                             cout << endl;
                             
                             cout << x << " - " << y << " = " << subtraction ( x, y ) << "\n\n";
                             
                             cin.get();
                             break;
                             
                        case 'm':
                        case 'M':
                             
                             cout << "multiplication: \n";
                             cout << "First number: \n>";
                             cin >> x;
                             cout << "Second number: \n";
                             cin >> y;
                             
                             cout << endl;
                             
                             cout << x << " * " << y << " = " << multiplication ( x, y ) << "\n\n";
                             
                             cin.get();
                             break;
                             
                        case 'd':
                        case 'D':
                             
                             cout << "division: \n";
                             cout << "First number: \n>";
                             cin >> x;
                             cout << "Second number: \n>";
                             cin >> y;
                             
                             cout << endl;
                             
                             if ( y == 0 ) 
                             {
                                  cout << "Error: \n\n";
                                  
                                  continue;
                             }
                             
                             cout << x << " / " << y << " = " << division ( x, y ) << "\n\n";
                             
                             cin.get();
                             break;
                             
                        default:
                                
                                cout << endl;
                                
                                cout << "Error: Invalid Choice. \n\n"; 
                                
                                cin.get();
                                break;
                 }
            }     
        }
        
        cin.get();   
        return ( 0 );
        
    }
        
    double addition ( double x, double y )
    {
           return x + y;
    }
    
    double subtraction ( double x, double y )
    {
           return x - y;      
    }
    
    double multiplication ( double x, double y )
    {
           return x * y;
    }
    
    double division ( double x, double y )
    {
           return x / y;
    }
    Just an idea........
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Cathalo View Post
    What they mean by indentation is that your code is broken up nicely so that it is easier to read.Like below its easier to see when varibles are being initialised and so on

    Code:
    #include<iostream>
    #include<string>
    
    int main(){
    
    int someVar=0;
    int anotherVar=1;
    int userInput;
    
    switch(userInput){
                         case 1:
                                    cout<<userInput<<endl;
                                    break;
    
                          case 2:
                                     cout<<userInput<<endl;
                                     break;
          }
    return 0;
    }
    I have only started out programming but if your code is presented so that other people can read it easily the programmers on this site are quick to answer either telling you where you went wrong or at least point you in the right direction.

    also where varibles have 2 (or more)words,capitalise the 2nd words first letter,it makes it the varible name standout more
    This isn't proper indentation either.
    Sshakey6791 got the indentation right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I probably would have used an array of function pointers, or, since this is C++, a std::map -- oh, and I definitely would have used tolower() -- but neither my solution nor yours is going to help the OP very much I would think. What are the chances someone is familiar with functions if they are not familiar with do-while loops?

    With that in mind, here are some links:


    One thing to note in Sshakey6791's code: it's a good idea to do as they have done and check for division by zero to avoid a segmentation fault . . . .

    BTW, "qoutient" is spelled "quotient". And you could reuse a "result" variable in your code instead of declaring sum, difference, etc.

    [edit]
    Sshakey6791 got the indentation right.
    More or less. This looks a little suspect to me.
    Code:
        do {
            
             cout << "Enter password: \n>";
             /* ... */
             
            } while ( passWord != thePassWord );
    But Sshakey6791 certainly knows what to do. That's probably just his/her style.
    [/edit]
    Last edited by dwks; 03-12-2009 at 04:34 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. code help :)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-28-2002, 01:12 PM
  5. Replies: 1
    Last Post: 01-23-2002, 03:34 AM