Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [HELP] Input verification troubles.
New Posts  All Forums:Forum Nav:

[HELP] Input verification troubles.

post #1 of 14
Thread Starter 
Here is a snippet of code:

int loopnum = 0;
int loopcount = 0;
double userinput = 0;
double total = 0;
cout << "Please enter the amount of resistors : ";
cin >> loopnum;
while (loopcount < loopnum){
cout << "Resistor " << loopcount +1 << " value : ";
cin >> userinput;
total = total + userinput;
++loopcount;
}
cout << "Total Value : " << total;


I need to make sure "userinput" is only positive. Basically keep asking for input until it is entered positive!

This is part of a huge code so I can't see straight right now. if-else would be too complicated to fit into this while loop I've got going, me thinks.

Help is greatly appreciated. Thsi is teh final step and due date is in 37 minutes!

Here is the original code it is going into:
Code:
cout << "Please input the Maximum voltage in the circuit, Vmax."<<endl<<endl;
    cout << "Vmax = ";cin>> Vmax ;cout<<" volts";  //asking the user to input the maximum voltage from the alternating source//
    cout << ""<<endl<<endl;
          
          cout << "Please enter the amount of resistors : ";
          cin >> loopnum;
          
          while (loopcount < loopnum +1)
          {
           cout << "Resistor " << loopcount << " value : ";
           cin >> userinput;
           Restotal = Restotal + userinput;
           ++loopcount;
          }
        
                VoT =(Vmax * sin(2*M_PI*freq*time));
                IAC = (VoT/(Restotal));  
                          
                           cout << "Please input the physical frequency in the circuit, freq."<<endl<<endl;
                           cout << "freq = ";cin>> freq ;cout<<" Hz";  //asking the user to input the physical frequency from the alternating source//
                           cout << ""<<endl<<endl;
    
         cout<<"Total Resistance is ";cout<<Restotal<<" ohms."<<endl<<endl;
               Tend = (1/freq);
               do
               {
               time+=0.002;
               VoT =(Vmax * sin(2*M_PI*freq*time));
               IAC = (VoT/(Restotal)); 
               cout<<"The current of the AC voltage is ";cout<<IAC<<" amperes."<<endl<<endl;
               }
                while (time <= Tend);
          break;
post #2 of 14
well i java you could force the user to input a positive number.

if(num < 0){
out.print("You Idiot I asked for a positive number);
run loop again
}
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
post #3 of 14
Thread Starter 
Quote:
Originally Posted by SS_Patrick View Post
well i java you could force the user to input a positive number.

if(num < 0){
out.print("You Idiot I asked for a positive number);
run loop again
}
Unfortunately, C++ isn't as easy and accessible as Java. basic commands they teash us is,
if (input<= 0)

{
output "enter positive no."
ask again for input.
}


And u have to loop this til it is valid..
post #4 of 14
Darn, sorry I know very limited c++
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
post #5 of 14
Sorry I just logged in and saw this, hope you have enough time.

Here it is in some pseudo code you can figure the rest out. You're going to want to nest a do while loop in your first loop

Code:
while (loopcount < loopnum +1)
          {
           cout << "Resistor " << loopcount << " value : ";
   do {
cin >> userinput;
if (userinput <= 0)
cout << "Please make sure the value is greater than 0" << endl;
   } while (userinput <= 0);
           Restotal = Restotal + userinput;
           ++loopcount;
          }
post #6 of 14
This seems to work without really having to modify your base code.
Bolded the parts that I had to add to the original example.
Seems to work correctly in DevC++.

Quote:
#include <iostream.h>

double invalidEntry();

int main()
{
int loopnum = 0;
int loopcount = 0;
double userinput = 0;
double total = 0;
cout << "Please enter the amount of resistors : ";
cin >> loopnum;

while (loopcount < loopnum)
{
cout << "Resistor " << loopcount +1 << " value : ";
cin >> userinput;
if (userinput < 0) userinput = invalidEntry();
total = total + userinput;
++loopcount;
}

cout << "Total Value : " << total << "\
\
";

system("pause");
return 0;
}

double invalidEntry()
{
int data = -1;

while (data<0)
{
cout << "ERROR: Please enter a positive number : ";
cin >> data;
}

return data;
}
Or you could just make it a nested loop if you haven't learned about functions yet.
Would be more of the following:

Quote:
while (loopcount < loopnum)
{
cout << "Resistor " << loopcount +1 << " value : ";
cin >> userinput;
while (userinput < 0 )
{
cout << "ERROR: Please enter a positive number : ";
cin >> userinput;
}

total = total + userinput;
++loopcount;
}
The advantage of the first solution is that you can use it for any situation where you need to check for a negative number. It ends up making your code a little more portable and re-usable.
Edited by beers - 3/4/11 at 7:02am
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
post #7 of 14
Quote:
Originally Posted by beers View Post
This seems to work without really having to modify your base code.
Bolded the parts that I had to add to the original example.
Seems to work correctly in DevC++.
You're doing double the work, I'm a refactoring nut and you're running a separate function (increasing unnecessary overhead), asking for input in multiple places... it's spaghetti, all you need to do is nest your input in a do while literally an addition of 4 lines, 2 if you don't want to tell the end user they need to insert a greater value.

Code:
do {
cin >> userinput;
if (userinput <= 0)
cout << "Please make sure the value is greater than 0" << endl;
   } while (userinput <= 0);
post #8 of 14
Thread Starter 
Thanks guys! Already submitted, but I'll keep this in mind for the next assignment!
post #9 of 14
Quote:
Originally Posted by CraigVella View Post
You're doing double the work, I'm a refactoring nut and you're running a separate function (increasing unnecessary overhead), asking for input in multiple places... it's spaghetti, all you need to do is nest your input in a do while literally an addition of 4 lines, 2 if you don't want to tell the end user they need to insert a greater value.
So you disagree to the fact that it's a good habit to get into creating code that is usable more than once?

Yes, it has a minute amount of extra overhead.
Yes, this project doesn't care about wasted CPU cycles.

If you're making a project where you constantly have to scrub each input for a positive value, you'd gain both time and consistency with this approach.
Quote:
it's spaghetti,
Seems like a logical flow of events to me, not sure how you can automatically label it spaghetti.
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
post #10 of 14
Quote:
Originally Posted by beers View Post
So you disagree to the fact that it's a good habit to get into creating code that is usable more than once?

Yes, it has a minute amount of extra overhead.
Yes, this project doesn't care about wasted CPU cycles.

If you're making a project where you constantly have to scrub each input for a positive value, you'd gain both time and consistency with this approach.

Seems like a logical flow of events to me, not sure how you can automatically label it spaghetti.
1) You're asking for input in two separate places to populate the value of 1 variable.

2) It's always a good idea to write code to do things in the least amount of CPU cycles, great practice

3) The solution of a nested loop, is considerably shorter to write

4) From what I see this is the only place input with such conditions are asked for, if you wanted it to be truely reusable you may want to throw some parameters on your function, call it something like double boundedInput(int lowBound, int highBound) and then it would be truely a reusable function

5) You're function is so short it should be defined as inline, that's just common coding practice for something like that.

It's doable both ways, but why not just do it the best way?
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] Input verification troubles.