How do I fix this?

Hello dear cpp users and developers. I'm pretty new to C++ and made my first program. It's a simple calculator that totals up 2 numbers ... the interesting thing about it is that it also validates the user's input.

Sadly, I have 3 problems, which I can't seem to figure out how to fix.

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
//Which libraries to include.
#include <iostream> 
#include <string>
//Namespace to be used.
using namespace std;
//Declared variables.
    unsigned int x;
    unsigned int y;
    unsigned int d;
    string e;
    string f;
//Validates if the integer input was a valid numeric value and if not gives off an error.
int validnumber(unsigned int d)     
{    
 while (cin.fail()) {
  if (!(cin >> x)) {
     cin.clear();
     cin.ignore();
    cerr << "Please type a number!\n";
    cin >> x;
  }
  else if (!(cin >> y)) {
     cin.clear();
     cin.ignore();
    cerr << "Please type a number!\n";
    cin >> y;
       }
 }
}
//Function to total up the two given numbers.
int sum(unsigned int a,unsigned int b)  
{
return a+b;
}
//Asks user for a repeat of the program.
void askrepeat()     
{
    cout << "If you want to put on a new calculation, please type \"Y\".\nIf you'd like to close the program type \"N\"." << endl;
     cout << endl;
    cin >> e;
}
//Validates if the string input was either Y or N and if not gives off an error.
string validstring(string f)    
{    
 while (e!="Y"&&e!="N") {
     cin.clear();
     cin.ignore();
    cerr << "Please type either \"Y\" or \"N\"!" << endl;
     cout << endl;
    cin >> e; }
  system("cls");
return e;
}
//Main program.
int main()      
{
 do {
    cout << "Please type two numbers, which you'd like to total up\nand confirm by pressing \"Enter\":" << endl;
     cout << endl;
    cin >> x;
   validnumber(x);
    cin >> y;
   validnumber(y);
     cout << endl;
    cout << "The result of " << x << " and " << y << " is: " << sum(x,y) << endl;
     cout << endl;
  askrepeat();
   validstring(e);
 } while (e == "Y");
}

/*
Errors:
       
       1) The "cerr" message is displayed all the time a chracter is not a numeric/alphabetic value. How can it be made, that the warning is displayed only once per wrong input?
       Fix?
       
       2) The validnumber() function does not properly work. If you input a string for "x", when it asks you for the numbers, and then input a number and then a number for "y", as well, it works flawlessly. But once you input a string for "y" (whatever you typed for "x" doesn't matter, since it'll be overwritten anyways...) the calculation seems to break and "x" will be overwritten? I don't understand.
       Solution?
       
       3) Within the validnumber() function, it's questionable if it's good to use "cin.fail()" as a condition for the while-statement, or so I think ...
       Answer?
       
*/


Is there any way I could fix these 3 errors? I can't come to the conclusion :(.
Thank you for your help & time!
It seems most of your errors would be coming from your function checking if the input is valid then trying to force it into one of the two variables you don't know is correct.

I would just suggest just making a function which just checks if the input is correct and returns a boolean (true or false) so you can deal with it somewhere else.

Using cin to get user input - http://www.cplusplus.com/forum/articles/6046/
I was able to fix it thanks to your tip, bluezor. The program works flawlessly now :). Thanks!
Topic archived. No new replies allowed.