Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with C++ looping; simple question concerning input of "0."
New Posts  All Forums:Forum Nav:

Help with C++ looping; simple question concerning input of "0."

post #1 of 13
Thread Starter 
Hello again,

Created a simple program requesting input of an integer, looping for an equivalent duration of the input, and displaying a "$" each time.

Assignment states only to request user input, which needs to be between 1-10; if not display error message and request user input again.

All fine and dandy here; however, the way I have coded it if the user inputs 0 the loop ends. If I didn't want the user to end the function at all, how could I set up this loop?

I prefer the way I created the program (even though it says nothing about terminating the loop in the assignment); but, for knowledge sake I was wondering.

I guess the question concerns looping not zero; apologies there.
Code:

#include <iostream> 
using namespace std;

int main ()
{
        int     input,                  // User input.
                output;                 // Output ($).

        // Request user input; validate; loop.
        do 
        {
                cout << "Enter the next number (1-10). 0 to end function." << endl;

                cin >> input;

                output = input;

                // Output if validated.
                if (input >= 1 && input <=10 )
                {

                        for ( output = 1; output <= input; output++ )
                        {
                                cout << "$";
                        }

                        cout << endl;
                }

                // Check user option to end function.
                else if (input == 0)

                        cout << "Function ended.";

                // Output error if not valid input, loop.
                else 

                        cout << "Error, ";
        }
        while (input != 0); // Terminates function at user request.

        cout << endl;

        system ("pause");

        return 0;


} // End function main.

Edited by di inferi - 11/3/12 at 9:44am
post #2 of 13
You could also make it an infinite loop and use continue for zero.
post #3 of 13
Thread Starter 
Quote:
Originally Posted by Makyl View Post

Use continue.

Ya, haven't learned that yet my friend. I will look into it now though; thanks!
post #4 of 13
Or break.

I'm not sure what you're asking here though.
post #5 of 13
use a set bit.....make another int in there that the input saves to. use the if block to set the "input" variable to != 0 and make the else loop set it to 0.
3770k
(19 items)
 
  
CPUMotherboardGraphicsRAM
i7-3770K  ASUS Maximus V FORMULA  EVGA GeForce GTX 680 Classified Corsair Dominator Platinum 1600 
Hard DriveCoolingCoolingCooling
Corsair Neutron Series GTX  EK-Supreme HF - Full EN  XSPC Dual 5.25 XSPC RX240 Dual 120mm  
CoolingCoolingCoolingCooling
X-Flow Black Ice Xtreme III Koolance 1/2 x 5/8 black compression Pmp-500  Cooler Master Sickle Flow 120m Blue LED 
CoolingCoolingOSKeyboard
EVGA 680 Classified HydroCopper Waterblock AeroCool Touch 2100 Windows 7 Ultimate Corsair Vengeance K90  
PowerCaseMouse
Rosewill LIGHTNING 1000 Corsair 800D Razer Naga Molten 
  hide details  
Reply
3770k
(19 items)
 
  
CPUMotherboardGraphicsRAM
i7-3770K  ASUS Maximus V FORMULA  EVGA GeForce GTX 680 Classified Corsair Dominator Platinum 1600 
Hard DriveCoolingCoolingCooling
Corsair Neutron Series GTX  EK-Supreme HF - Full EN  XSPC Dual 5.25 XSPC RX240 Dual 120mm  
CoolingCoolingCoolingCooling
X-Flow Black Ice Xtreme III Koolance 1/2 x 5/8 black compression Pmp-500  Cooler Master Sickle Flow 120m Blue LED 
CoolingCoolingOSKeyboard
EVGA 680 Classified HydroCopper Waterblock AeroCool Touch 2100 Windows 7 Ultimate Corsair Vengeance K90  
PowerCaseMouse
Rosewill LIGHTNING 1000 Corsair 800D Razer Naga Molten 
  hide details  
Reply
post #6 of 13
Thread Starter 
Quote:
Originally Posted by fogran View Post

use a set bit.....make another int in there that the input saves to. use the if block to set the "input" variable to != 0 and make the else loop set it to 0.

I am not quite sure what you mean here; haven't learned set bits yet.

Lets say I scraped everything I have now; and created an infinite loop that just kept requesting user input of an integer between 1-10 (regardless of whether or not input was zero). It displays an error message when out of range, then continues back to the top of the loop and requests user input again.

I can't thank of a way to do that with a for loop; (unless using i as the checked variable and set it to some ridiculously high number).

I guess my question is how could I do this with a do while or while loop. I have no clue what I could use for the while condition.

I apologize for my lack of knowledge and expressing what I am trying to learn lol. I am a newbie.

Thank you all for your help so far.
post #7 of 13
Use a do while like you have, except set
Code:
while(true)

Then you'll use an if statement to check if the input is valid, then if it's not, check if it is zero. If it is zero, run a break; statement and you'll be fine.
Xiggy
(10 items)
 
Crate
(10 items)
 
Thunder
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 2600K ASRock z77 extreme 3 NVIDIA GeForce GTX 560 Ti Corsair  
Hard DriveHard DriveCoolingOS
Western Digital Western Digital Thermaltake Frio Windows 7 Home Premium 
PowerCase
OCZ Fatal1ty 750w Xigmatek Utgard Window 
CPUMotherboardGraphicsGraphics
AMD Opteron 180 MSI K8N SLI-F EVGA 9800GTX+ EVGA 9800GTX+ 
RAMHard DriveCoolingOS
DDR 200 OCZ Vertex 3 SSD Swiftech Polaris Linux Mint 14 
PowerCase
hec 650w Milk Crate 
CPUMotherboardGraphicsRAM
Phenom II x4 840 Asus m4a78lt-m VisionTek Radeon HD 5450 Corsair ValueSelect 1333 2GB 
Hard DriveOSMonitorKeyboard
160GB 5400rpm Windows 7 Home Premium Dell 19" Full Logitech 
PowerCaseMouse
535w Generic Thermaltake v4 Black Edition Logitech 
  hide details  
Reply
Xiggy
(10 items)
 
Crate
(10 items)
 
Thunder
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 2600K ASRock z77 extreme 3 NVIDIA GeForce GTX 560 Ti Corsair  
Hard DriveHard DriveCoolingOS
Western Digital Western Digital Thermaltake Frio Windows 7 Home Premium 
PowerCase
OCZ Fatal1ty 750w Xigmatek Utgard Window 
CPUMotherboardGraphicsGraphics
AMD Opteron 180 MSI K8N SLI-F EVGA 9800GTX+ EVGA 9800GTX+ 
RAMHard DriveCoolingOS
DDR 200 OCZ Vertex 3 SSD Swiftech Polaris Linux Mint 14 
PowerCase
hec 650w Milk Crate 
CPUMotherboardGraphicsRAM
Phenom II x4 840 Asus m4a78lt-m VisionTek Radeon HD 5450 Corsair ValueSelect 1333 2GB 
Hard DriveOSMonitorKeyboard
160GB 5400rpm Windows 7 Home Premium Dell 19" Full Logitech 
PowerCaseMouse
535w Generic Thermaltake v4 Black Edition Logitech 
  hide details  
Reply
post #8 of 13
Code:
#include <iostream> 
using namespace std;

int main ()
{
        int     input,                  // User input.
                output;                 // Output ($).
                exitCondition = 0;                                <---added

        // Request user input; validate; loop.
        do 
        {
                cout << "Enter the next number (1-10). 0 to end function." << endl;

                cin >> input;

                output = input;

                // Output if validated.
                if (input >= 1 && input <=10 )
                {

                        for ( output = 1; output <= input; output++ )
                        {
                                cout << "$";
                        }

                        cout << endl;
                }

                // Check user option to end function.
                else if (input == 0)
                         exitCondition = 1;                             <----------------------added
                        cout << "Function ended.";

                // Output error if not valid input, loop.
                else 

                        cout << "Error, ";
        }
        while (!exitCondition); // Terminates function at user request.                 <-------------------------Changed

        cout << endl;

        system ("pause");

        return 0;


} // End function main.




i didnt mean an actual exit bit....sorry bad wording
3770k
(19 items)
 
  
CPUMotherboardGraphicsRAM
i7-3770K  ASUS Maximus V FORMULA  EVGA GeForce GTX 680 Classified Corsair Dominator Platinum 1600 
Hard DriveCoolingCoolingCooling
Corsair Neutron Series GTX  EK-Supreme HF - Full EN  XSPC Dual 5.25 XSPC RX240 Dual 120mm  
CoolingCoolingCoolingCooling
X-Flow Black Ice Xtreme III Koolance 1/2 x 5/8 black compression Pmp-500  Cooler Master Sickle Flow 120m Blue LED 
CoolingCoolingOSKeyboard
EVGA 680 Classified HydroCopper Waterblock AeroCool Touch 2100 Windows 7 Ultimate Corsair Vengeance K90  
PowerCaseMouse
Rosewill LIGHTNING 1000 Corsair 800D Razer Naga Molten 
  hide details  
Reply
3770k
(19 items)
 
  
CPUMotherboardGraphicsRAM
i7-3770K  ASUS Maximus V FORMULA  EVGA GeForce GTX 680 Classified Corsair Dominator Platinum 1600 
Hard DriveCoolingCoolingCooling
Corsair Neutron Series GTX  EK-Supreme HF - Full EN  XSPC Dual 5.25 XSPC RX240 Dual 120mm  
CoolingCoolingCoolingCooling
X-Flow Black Ice Xtreme III Koolance 1/2 x 5/8 black compression Pmp-500  Cooler Master Sickle Flow 120m Blue LED 
CoolingCoolingOSKeyboard
EVGA 680 Classified HydroCopper Waterblock AeroCool Touch 2100 Windows 7 Ultimate Corsair Vengeance K90  
PowerCaseMouse
Rosewill LIGHTNING 1000 Corsair 800D Razer Naga Molten 
  hide details  
Reply
post #9 of 13
Thread Starter 
Quote:
Originally Posted by poizone View Post

Use a do while like you have, except set
Code:
while(true)
Then you'll use an if statement to check if the input is valid, then if it's not, check if it is zero. If it is zero, run a break; statement and you'll be fine.

Ahhhh, so I made true the while condition, removed anything concerning zero ending the loop, and regardless of input it runs infinitely.

Is setting the while condition to true just sort of a catch all "always run this" condition?

Thank you all for the help.
post #10 of 13
Quote:
Originally Posted by di inferi View Post

Quote:
Originally Posted by fogran View Post

use a set bit.....make another int in there that the input saves to. use the if block to set the "input" variable to != 0 and make the else loop set it to 0.

I am not quite sure what you mean here; haven't learned set bits yet.

Lets say I scraped everything I have now; and created an infinite loop that just kept requesting user input of an integer between 1-10 (regardless of whether or not input was zero). It displays an error message when out of range, then continues back to the top of the loop and requests user input again.

I can't thank of a way to do that with a for loop; (unless using i as the checked variable and set it to some ridiculously high number).

I guess my question is how could I do this with a do while or while loop. I have no clue what I could use for the while condition.

I apologize for my lack of knowledge and expressing what I am trying to learn lol. I am a newbie.

Thank you all for your help so far.
OH!

http://pastebin.com/Jif26M3h
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with C++ looping; simple question concerning input of "0."