Beginner question on a 'While'

This is a discussion on Beginner question on a 'While' within the C++ Programming forums, part of the General Programming Boards category; Hey there people. Im sure its a silly mistake but i cant get my while to work. sorry to dump ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    Beginner question on a 'While'

    Hey there people. Im sure its a silly mistake but i cant get my while to work.

    sorry to dump a large code in your faces

    tell me off if you need to

    Code:
    #include <iostream.h>
    
    float FindVolume(int diameter,int length)
              {
                         float pi = 3.14159265358979323846;
                         float circumference = pi*diameter;
                         float volume = circumference*length;
                         return volume;
              }
    
    
    int SquareThisNumber(int number)
              {
                         int SquaredNumber = number*number;
                         return SquaredNumber;
              }
    
    int main()
              {
    
                         int diameter;
                         int length;
                         int number;
                         int choice;
                         int exit = 0;
    
    
          while(exit==0);  //Should loop because exit is specified as 0 above.
                           //exit can only be changed by user input at end of program.
                {
    
    
               std::cout << "Choose what you want to do from the list below and enter the number" << std::endl;
               std::cout << "1)Square a Number" << std::endl;
               std::cout << "2)Find the Diameter of a Cylinder" << std::endl;
               std::cin >> choice;
    
               if(choice==1)
                         {
                            std::cout << "Please enter a number. I would love to square it." << std::endl;
                            std::cin >> number;
                            std::cout << number << " squared is: " << SquareThisNumber(number) << std::endl;
                         }
    
               else if(choice==2)
                         {
                            std::cout << "Type the diameter of a cylinder" << std::endl;
                            std::cin >> diameter;
                            std::cout << "Type the length of a cylinder" << std::endl;
                            std::cin >> length;
                            std::cout << "Diameter: " << diameter << "cm" << std::endl;
                            std::cout << "Length: " << length << "cm" << std::endl;
                            std::cout << "The volume is " << FindVolume(diameter,length) << "cm3" << std::endl;
                         }
    
               else
                         {
                            std::cout << "WTF! enter a number from the list above! foolish...";
                         }
    
    
               std::cout << "What would you like to do?" << std::endl;
               std::cout << "0)Back to menu" << std::endl;
               std::cout << "1)Exit Program" << std::endl;
               std::cin >> exit;
                 }
    
      return 0;
    }

    Thanks for taking the time to click my post

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    look harder at your "While" statement
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Code:
    while(exit==0); // Notice the semicolon?  Infinite loop!
    Don't worry, its happend to all of us!

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Also,

    I suggest you use standard headers.
    For a simple fix
    Code:
    #include <iostream>
    instead of
    Code:
    #include <iostream.h>
    You are already doing std:: so you dont need using namespace std.
    What is C++?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    ah ha took the ; off and it worked silly me :s

    thanks for the help guys

  6. #6
    Banned
    Join Date
    Aug 2004
    Posts
    128
    add using namespace std;
    if you include this you wont have to type std::
    in front of all that junk..

    also why do you have the else staements, why not just leave the two as if, and set the last one as

    Code:
    if(blah != 1 && blah != 2)
    {
         return 0;
    }
    
    
    
    EX.
    
    if(choice==1)
    {
          std::cout << "Please enter a number. I would love to square it." << std::endl;
          std::cin >> number;
          std::cout << number << " squared is: " << SquareThisNumber(number) << std::endl;
    }
    
    if(choice==2)
    {
           std::cout << "Type the diameter of a cylinder" << std::endl;
           std::cin >> diameter;
           std::cout << "Type the length of a cylinder" << std::endl;
           std::cin >> length;
           std::cout << "Diameter: " << diameter << "cm" << std::endl;
           std::cout << "Length: " << length << "cm" << std::endl;
           std::cout << "The volume is " << FindVolume (diameter,length) << "cm3" << std::endl;
    }
    
    if(choice != 1 && choice != 2)
    {
           std::cout << "WTF! enter a number from the list above! foolish...";
    }

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Um, maybe because it is more readable and requires less typing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

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