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

[HELP] Input verification troubles. - Page 2

post #11 of 14
Only, it's not the best way.
If you want to adhere to this project specifically, just use fabs to find the absolute value. After all, why rewrite code that already exists?

The way outlined is more consistent with the style of coding of the project. It should be clear to you that this is a lower level introductory course to C++, the appropriate habits and techniques will be learned when applicable. Pasting together a bunch of different styles of code indicates the code may have not originated from the target individual, and may only serve to confuse a beginner.
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 #12 of 14
His original code ends with 'break;' which means he most likely is in a loop, naturally making his other loops nested by default, or even if he's in a switch or something he's understanding the concept used to nesting types of code. Dont even get me started on the use of break if it is in a loop. But it seems to me that nesting is something that he knows about.

I've taught several college level programming classes, nesting is something that gets taught usually the same day of writing your first loop. It's such a common practice.

Let's say you did want to use your code though, you are doing double the work, rename your function to getUserInput, or getPositiveUserInput, something you can understand then call it as follows

Code:
while (loopcount < loopnum)
{
cout << "Resistor " << loopcount +1 << " value : ";
userinput = getPositiveUserInput();
total = total + userinput;
++loopcount;
}
Now at least you're not doing double the entry asking and your code is much more readable. Still, nested loop best practice and if you do use this as I said before, it should be an inline function.
Edited by CraigVella - 3/4/11 at 7:46am
post #13 of 14
Thread Starter 
Quote:
Originally Posted by CraigVella View Post
His original code ends with 'break;' which means he most likely is in a loop, naturally making his other loops nested by default, or even if he's in a switch or something he's understanding the concept used to nesting types of code. Dont even get me started on the use of break if it is in a loop. But it seems to me that nesting is something that he knows about.

I've taught several college level programming classes, nesting is something that gets taught usually the same day of writing your first loop. It's such a common practice.
Why hello there, would u consider tutoring online for a compensation? Skype and free time is all you need! I'm also not completely stupid and won't need that much help!
post #14 of 14
You both are correct in the way you decided to program this problem. That's the beauty of programming, there is no one correct way of doing something. Beers, you had the right idea in trying to implement reusability in code by adding a function but honestly, its not the way I would have done things. Not saying you're wrong, just giving my view on the matter. I know he already submitted the assignment but I'm just going to through out my version of the code (frankly because I'm bored and I love programming ).

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)
          {
           cout << "Resistor " << loopcount << " value : ";
           cin >> userinput;
           loopcount++;

                if(userinput < 0) {
                     cout << "Please enter a positive number." <<endl;
                     userinput = 0;
                     loopcount--;
                }

           Restotal += userinput;
          }
So everything that is boled is what I changed and I'll explain why in order. In the while loop I simply said while loopcount is less then or equal to and removed the + 1 you were adding to loopnum (this will require less CPU cycles and be more efficient in terms of time). I moved where you increment loopcount so my algorithm works. The if statement tests if the userinput is less then 0, if it is tell the user they must enter a number that is 0 or greater. It then sets userinput to zero and decrements the loopcount variable. This way when you add userinput to the Restotal variable it wont affect the value and we can still keep the basic structure of the while loop. I also changed Restotal = Restotal + userinput to Restotal += userinput, it essential does the same thing but with less typing. The best programmers are the laziest. Although I said no one was wrong in how they wanted to implement this solution this is the most efficent way in terms of time. Everyone above is using a nested loop to test for validation while I used a nested if statement. This means that all of their algorithms are O(n^2) where mine is O(n), which really doesn't matter unless your running this on an ancient machine or this is a code snippit from a million line program, but its always good practice to implement the most efficient code.

Quote:
Originally Posted by MeeMoo220 View Post
Why hello there, would u consider tutoring online for a compensation? Skype and free time is all you need! I'm also not completely stupid and won't need that much help!
I know this wasn't directed to me but I'll offer it anyway. I'm in my 3rd year of a software development major and I love programming. I also love to see others learn about programming. I would be more then happy to help you out with any problems you have programming. Just send me a PM and I will hop on Skpye as soon as I'm able. (I have a lot more free time right now then usual as I just started spring break.)

I hope all this was helpful and shed some light on how to implement user input validation. Hopefully next time I can help BEFORE you submit the program.

Side note:

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
}
Quote:
Originally Posted by MeeMoo220 View Post
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..
Java and C++ are the same in terms of variable declaration, control structures and basic input output. The main place that they differ is how you implement objects the API that is available to you to use while programming and how the compiler and runtime environment works.
baby cakes
(13 items)
 
Macbook 4,1
(13 items)
 
 
CPUMotherboardGraphicsRAM
i7 930 [4.01Ghz vCore 1.28125 HT on] GA-X58A-UD3R ZOTAC GTX 460 1GB SLI [840/1680/1900 @stock volts] CORSAIR XMS3 6GB (3 x 2GB) 1600Mhz 
Hard DriveOptical DriveOSMonitor
C300 64GB SATA III, 2x F3 1TB 7200rpm RAID 0 Sony Optiarc 24X DVD/CD Ubuntu 14.04 22" Dell SP2009W 1680x1050 
PowerCase
CORSAIR HX 750W Cooler Master HAF 922 
CPUMotherboardGraphicsRAM
Core 2 Duo 2.4Ghz Penryn T8300 Some logical board GMA X3100 4GB (2x2GB) DDR2 667Mhz 
Hard DriveOptical DriveOSMonitor
Hitachi 160 GB 5400 RPM SATA II Combo drive OSX 10.6.4 (Snow leopard) 13.3" 1280x800 TFT 
PowerCase
Dying battery White macbook case 
  hide details  
Reply
baby cakes
(13 items)
 
Macbook 4,1
(13 items)
 
 
CPUMotherboardGraphicsRAM
i7 930 [4.01Ghz vCore 1.28125 HT on] GA-X58A-UD3R ZOTAC GTX 460 1GB SLI [840/1680/1900 @stock volts] CORSAIR XMS3 6GB (3 x 2GB) 1600Mhz 
Hard DriveOptical DriveOSMonitor
C300 64GB SATA III, 2x F3 1TB 7200rpm RAID 0 Sony Optiarc 24X DVD/CD Ubuntu 14.04 22" Dell SP2009W 1680x1050 
PowerCase
CORSAIR HX 750W Cooler Master HAF 922 
CPUMotherboardGraphicsRAM
Core 2 Duo 2.4Ghz Penryn T8300 Some logical board GMA X3100 4GB (2x2GB) DDR2 667Mhz 
Hard DriveOptical DriveOSMonitor
Hitachi 160 GB 5400 RPM SATA II Combo drive OSX 10.6.4 (Snow leopard) 13.3" 1280x800 TFT 
PowerCase
Dying battery White macbook case 
  hide details  
Reply
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.