Do While Looping Structure

This is a discussion on Do While Looping Structure within the C++ Programming forums, part of the General Programming Boards category; Please help me with a Do While loop. I have to promp the user to select a loop structure using ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    Do While Looping Structure

    Please help me with a Do While loop.
    I have to promp the user to select a loop structure using a Do While loop.

    User may choose from While "W", Do While "D", or For "F".
    Code must restrict the user to enter a valid selection. If the user's selection is invalid, the function clears the screen and re_displays a menu. This will be repeated until the user enters a valid selection.



    This is what I have so far and it doesnt run at all. Please help.




    Code:
    #include<iostream> 
    #include<stdlib.h> 
    
    using namespace std; 
    
    void main() 
    { 
    void DisplayBanner(); 
    int PromptUserForSelection(); 
    
    
    
       DisplayBanner(); 
    PromptUserForSelection(); 
       
    
    } 
    
    void DisplayBanner() 
    
    { 
       system("cls"); 
       cout<<"    PROGRAM:         Labs456\n\n"; 
       cout<<"     AUTHOR:         JT\n"; 
       cout<<"       DATE:         Saturday July 7, 2007\n"; 
       cout<<"   COMPILER:         Visual C++ using VS2005\n"; 
       cout<<"   COMPUTER:         HP PC\n"; 
       cout<<"    VERSION:         VX.0\n"; 
       cout<<"    PURPOSE:         Promting the user to enter a selection.\n"; 
       cout<<"\n\n\t\t Press <Enter> to Continue"; 
       cin.get(); 
        
    } 
    int PromptUserForSelection() 
    
    { 
       char selection, 'W', 'D', 'F'; 
        
    
       do { 
    
    cout << "\n\n";  
       cout << "Structure      Selection\n\n";  
        
       cout << "While              W" << endl; 
       cout << "Do While           D" << endl; 
       cout << "For                F\n\n" << endl; 
       cout << "Enter Selection <Q to Quit>: "; 
        
    cin >> selection; 
       cout << endl; 
       } 
       
       while (selection != W ||  D || F); 
    
        if (selection = W) 
        cout << "You have chosen While" << endl; 
    
        if (selection = D)  
      cout << "You have chosen Do While" << endl; 
       
        if (selection = F) 
      cout << "You have chosen For" << endl; 
       
      return 0; 
       
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include<stdlib.h>
    you don't need this. stdio.h is for C programs. If you want to use the functions listed in stdio.h in a C++ program (which you aren't) then use cstdio
    Code:
     int main() //see faq
    { 
      void DisplayBanner(); 
      int PromptUserForSelection(); 
    
    
    
      DisplayBanner(); 
      PromptUserForSelection(); 
       
      return 0;
    } 
    
    ...
    
    int PromptUserForSelection() 
    
    { 
       char selection, 'W', 'D', 'F'; //'W', 'D', and 'F' are character constants. What are you trying to do here?
    
    ...
    
       while (selection != 'W' || selection != 'D' || selection != 'F'); // single quotes is for a character literal
    
        if (selection == 'W') // = assigns, == compares. also, the quotes again.
        cout << "You have chosen While" << endl; 
    
        if (selection == 'D')  
      cout << "You have chosen Do While" << endl; 
       
        if (selection == 'F') 
      cout << "You have chosen For" << endl; 
       
      return 0; 
       
    }
    Last edited by robwhit; 07-18-2007 at 09:02 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    please see my comments throughout your code
    Code:
    #include<iostream> 
    #include<stdlib.h>  // you shouldnt need this, remove it.
    
    using namespace std; 
    
    void DisplayBanner();  // i moved your function prototypes outside and above main (where they need to be)
    int PromptUserForSelection(); // side note, i wouldnt start my function (or variable) names with capitals, use something like displayBanner and promptUserForSelection instead
    
    void main() // main should be int not void
    { 
       DisplayBanner(); 
       PromptUserForSelection(); 
       
      // since main is now int, add a 'return 0;' here to tell the OS your program is done
      return 0;
    } 
    
    void DisplayBanner() 
    { 
       system("cls"); // this statement makes your program windows-only! just FYI (for a simple program like this i would do the same thing)
       cout<<"    PROGRAM:         Labs456\n\n"; 
       cout<<"     AUTHOR:         JT\n"; 
       cout<<"       DATE:         Saturday July 7, 2007\n"; 
       cout<<"   COMPILER:         Visual C++ using VS2005\n"; 
       cout<<"   COMPUTER:         HP PC\n"; 
       cout<<"    VERSION:         VX.0\n"; 
       cout<<"    PURPOSE:         Promting the user to enter a selection.\n"; 
       cout<<"\n\n\t\t Press <Enter> to Continue"; 
       cin.get(); 
        
    } 
    int PromptUserForSelection() 
    
    { 
       char selection, 'W', 'D', 'F'; // W T F? :p remove the single quotes..
    // what are you trying to do, declare 4 char variables (selection, w, d, f) or what?
        
       do { 
    
    cout << "\n\n";  
       cout << "Structure      Selection\n\n";  
        
       cout << "While              W" << endl; 
       cout << "Do While           D" << endl; 
       cout << "For                F\n\n" << endl; 
       cout << "Enter Selection <Q to Quit>: "; 
        
    cin >> selection; 
       cout << endl; 
       } 
       
       while (selection != W ||  D || F); // unlike a language like visual basic, you have to reiterate the entire
    // statement, that is: selection != W && selection != F, etc)
    
        if (selection = W) // this will assign W to selection, to compare variables use ==
        cout << "You have chosen While" << endl; 
    // use an if-else block:
       else if (selection = D)  
      cout << "You have chosen Do While" << endl; 
       else if (selection = F) 
      cout << "You have chosen For" << endl; 
       
      return 0; 
       
    }
    i would STRONGLY suggest reading some tutorials on this website (or any website) to get your basic C++ knowledge down.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
       char selection, 'W', 'D', 'F'; // W T F? :p remove the single quotes..
    // ...
        if (selection = W) // this will assign W to selection, to compare variables use ==
        cout << "You have chosen While" << endl; 
    // use an if-else block:
       else if (selection = D)  
      cout << "You have chosen Do While" << endl; 
       else if (selection = F) 
      cout << "You have chosen For" << endl;
    I think the OP wants something akin to this.
    Code:
    char selection;
    
    // ...
    
        if (selection == 'W') // this will assign W to selection, to compare variables use ==
        cout << "You have chosen While" << endl; 
    // use an if-else block:
       else if (selection == 'D')  
      cout << "You have chosen Do While" << endl; 
       else if (selection == 'F') 
      cout << "You have chosen For" << endl;
    [edit] As robwit said. [/edit]

    Code:
    #include<stdlib.h>  // you shouldnt need this, remove it.
    Actually, it is needed if you use system(). In C++, it should be <cstdlib>, though.

    Code:
    void DisplayBanner();  // i moved your function prototypes outside and above main (where they need to be)
    int PromptUserForSelection(); // side note, i wouldnt start my function (or variable) names \
    with capitals, use something like displayBanner and promptUserForSelection instead
    Function prototypes can be present in any block. If they are, their scope is limited to that block. This means that if you put some prototypes in main(), those functions will be prototyped within main(), but inside other functions they will not be.

    As for the capitalization, there are many programming styles. Many programmers I can think of like using camel notation (that's thisIsAnIdentifier) with initial capitals. I prefer "underscore notation" myself: this_is_an_identifier. There are as many styles as there are programmers. Well, there would be if there weren't some standards. But you can use whatever style you like, but please be consistent.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    every function ive written (in C/C++) has been outside any other function, and i would think is the most common and proper way, since there is no reason he _needs_ it declared inside main. i was just pointing it out, as it seems he put it there because he thought it had to go there and should be there, im pointing it out so he knows and so his way above doesnt become a habit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure Array Looping Problem
    By Vitamin_C in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 02:22 PM
  2. Looping switch structure Help (newbie)
    By Monte2 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 02:16 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

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