Validation

I am writing a program that corresponds to one of my other classes, of which is circuit analysis, but i need the user to enter only numbers when it asks for the values, i can't seem to find anything on google, I could use isdigit but that only checks the first digit, i've looked at alot of things but they are all very confusing.(I did just start this class last month)but we are supposed to loop the question until they enter the correct value(s).

here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//Mark Gilbert
//2/04/10
//EET Tech Programming
//Time Domain Reflectometry
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
const int SIZE = 1; 
double Rl,Ro,Vr,Va,Cl,def;
char ans;

/*      Where Cl = the reflection coefficient
        Rl = the load resistance
        R0 = the characteristic impedance of the cable
*/

void Reflected()
{
    
    cout << "please enter the Reflected Pulse Value:";
    cin >> Vr;  
    if (!isdigit(Vr))
       cout << "rawr";
    
    Cl=Vr/Va;
    cout << "The Coefficient is:" << Cl << "\n";
    
    Rl=((-Ro-(Cl*Ro))/(-1+Cl));
    cout << "The load impedance is:" <<  Rl << "\n";
    cout << "Enter ""r"" to restart:";
    def =1;
    cin >> ans;
    
}
 void Impedance()
{
    cout << "please enter the Reflected Pulse Value:";

    cin >> Vr;       
    Cl=Vr/Va;
    cout << Cl << "\n";
    
    Rl=((-Ro-(Cl*Ro))/(-1+Cl));
    cout << Rl;
    
    cout << "Enter ""r"" to restart:";
    def =1;
    cin >> ans;
           
}
main()
{
      char choice[SIZE];
      
      do      {
      def = 0;
      system("cls");
      cout << "What is the Impedeance of the line\n";
      cin >> Ro;
      if (Ro < 0)
         {
         cout << "Please enter the correct resistance!\n";
         main();
         }
      cout << "please enter the Voltage Applied\n";
      cin >> Va;
      cout << "Type I for Impedance or Type R for Reflected\n";
      
      while (def == 0){
      cin >> choice;
      if (!stricmp(choice,"I") >0)
         Impedance();
    else if (!stricmp(choice,"R") >0)
         Reflected();
    else{
        cout << "you entererd a wrong value!\n";
        def = 0;
        }
        }
     
      
      }while(toupper(ans) == 'R');
}
      
      



of course there are errors in the code but i am still working on that. i just need a way to validate the use of numeric characters only.
where it says:
1
2
 if (!isdigit(Vr))
       cout << "rawr";

is what i am currently working on. pretty much ignore that part
Firstly, that indentation needs big improvements. It's hard to read your code because it's very untidy and your indentation needs to be more consistant.
Secondly, don't use the system() function. http://cplusplus.com/forum/articles/11153/
Thirdly, don't use global variables. You don't need them.
</constructive criticism>

You want the user to only be able to enter numbers, right? Where are these numbers going?
I need the user to enter only number in the variable Vr so that the calculation doesn't get messed up. I am doing Time Domain Reflectometry. the only time i need a variable is when they decide either to enter the reflected pulse voltage or Vr or the Rl, the system("cls") was given to me by my professor, and i was planning on removing the global variables down to int main() i just haven't got that fair and i was of course going to tidy up after the program works. is that what you were looking for
Ok. I'm glad you plan to tidy that up, and I'm glad you're going to get rid of the globals, but you didn't answer my question. Where are the numbers going, i.e. into a variable, string, or array? Do you just want to store a number in one of those doubles you've got up there, and check that it's a real number (no pun intended)?

Oh, and your professor actually recommended you to use system("cls")? I've been programming for about a year and I know not to use it! Your professor is bad and he should feel bad. Don't use it (system(), that is). It's evil. Refer to that article I linked you to... Perhaps he could do with a good read of it, too.

Right, on to your code.

cout << "Enter ""r"" to restart:";
You will get errors with this line. I take it you want the output to look like
Enter "r" to restart: 
, yes? In that case, you need to escape the quotation mark, because otherwise the compiler will take that as an end-of-string; and throw errors at you. Example:
std::cout << "\"This text will be enclosed in quotation marks.\"";
You see the backslash? That escapes the quotation mark so that the compiler knows it's part of the string literal.

Anyway, if you want to check that the user entered a number, you'll need to use something other than std::cin, like stringstreams.
Here is an updated version as of like two minutes ago it has no errors and it runs if i dont put characters into the variables and no the varables go to the calulations and and turn into the end result of another variable which is displayed as Rl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Mark Gilbert
//2/04/10
//EET Tech Programming
//Time Domain Reflectometry
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
const int SIZE = 1; 
double Rl,Ro,Vr,Va,Cl,def;
char ans;

/*      Where Cl = the reflection coefficient
        Rl = the load resistance
        R0 = the characteristic impedance of the cable
*/

void Reflected() //calculates the Reflected load
{
    
    cout << "Please enter the Reflected Pulse Value:";
    cin >> Vr;
    if (Vr)
    Cl=Vr/Va;
    cout << "The Coefficient value is:" << Cl << "\n";
    Rl =((-Ro-(Cl*Ro))/(-1+Cl));
    cout << "The Load Impedance is:" <<  Rl << "\n";
    cout << "Enter ""r"" to restart:";
    def = 1;
    cin >> ans;
    
}
 void Impedance() //calculates the load impedance
{
    //nothing here YET 
    /* also ensure that the reflected pulse value entered is equal to or less than the absolute value of the magnitude
    of the applied pulse. If the magnitude entered is larger than the applied pulse,
    prompt the operator to re-enter the reflected pulse value.      */  
}
main()
{
      char choice[SIZE];
      
      do      {
              def = 0;
              system("cls");
              cout << "What is the Impedeance of the line\n";
              cin >> Ro;
              if (Ro < 0)
              {
                 cout << "Please enter the correct resistance!\n";
                 main();
              }
              cout << "please enter the Voltage Applied\n";
              cin >> Va;
              cout << "Type I for Impedance or Type R for Reflected\n";
      
              while (def == 0)
                    {
                    cin >> choice;
                    if (!stricmp(choice,"I") >0)
                          Impedance();
                    else if (!stricmp(choice,"R") >0)
                          Reflected();
                    else {
                          cout << "you entererd a wrong value!\n";
                          def = 0;
                         }
                    }
     
              }
      while(toupper(ans) == 'R');
}
      
      


and yes i want them to see "r" and that is what my compiler is showing me when it compiles and runs.
You should not call main() ever, from anywhere in your program. (Line #53)
Okay, im sorry to bother all of you if i do i don't want nit-picking of my code. that is a temporary thing anyways this isn't completed work as i have said before. it was faster to do that than to write a loop at the time.
You are missing the point(s). The responses you have received so far are important.

However, you should have received an answer to your question by now.

If you want to make absolutely sure that the user entered nothing except an integer value, you must read it as a string (using getline()) and then convert it to a stringstream, use the extraction operator (>>), and check to make sure that your stringstream is at EOF.

I have a fancy example of doing this here:
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827

The parts that you are interested in are lines 29..41. After line 41 you have to add what you want to happen if the user did not enter an integer value.


If you can put up with garbage on the line after the integer, then you can just check that cin is not in an error state:
http://www.cplusplus.com/forum/beginner/18258/#msg92955

Hope this helps.
Topic archived. No new replies allowed.