making functions work together

so I was told to do the following:

Reproduce the function isValidInt to validate the format for
an integer which has been entered using the keyboard.

Test your function using the following test cases:

-1234
5674.25
$1700

Reproduce the function readInt that uses isValidInt.
Demonstrate your function.

Write a function isValidDouble to validate the format for
a real number which has been entered using the keyboard.

Test your function using the following test cases:

-23486.33
.67425
$24576.79
54,765.98
76.543.20

Write a function readDouble that uses isValidDouble.
Demonstrate your function.

But as you will see from my code I can not figure out how to tie it all to gather to use these functions I have made. If some one could show me how I would greatly appreciate it!

Note: it keeps telling me "invalid selection please try again" even when I put type the right letters in....

also how do I keep it from going into an infinite loop?

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//  main.cpp
//  Program 11
//  Created by William Blake Harp on 8/6/14.

#include <iostream>

using namespace std;

bool isvalidInt(string);
int readInt(string prompt);
double readDouble(string prompt);

int main()
{
    int number;
    char selection;
    
    do
    {
        cout << "Enter 'I' for integer or 'D' for double: " << endl;
        cin >> selection;
    
        if (selection == 'i' || selection == 'I')
        {
            number = readInt("Enter an int: ");
        }
        else if(selection == 'd' || selection == 'D')
        {
            number = readDouble("Enter a double int: ");
        }
        else
        {
            cout << "invalid selection please try again\n\n";
            
        }
    }while (selection != 'i' || selection != 'I' || selection != 'd' || selection != 'D');
        
    return 0;
}// End Main.

bool isvalidInt(string str)
{
    int start = 0;          //start position in the string
    int i;                  //position in the string
    bool valid = true;      //assume a valid integer
    bool sign = false;      //assume no sign
    
    //check for an empty string
    if (str.length() == 0) valid = false;
    
    if (valid)
        if (str.at(0) == '-' || str.at(0) == '+')
        {
            sign = true;
            start = 1;
            
            if (sign && str.length() == 1)
                valid = false;
        }
        else if(! isdigit(str.at(0)))
        {
            valid = false;
        }
    return 0;
}// End is valid int.

int readInt(string prompt)
{
    string strVal("");
    
    do
    {
        cout << prompt;
        getline(cin, strVal);
    } while (! isvalidInt(strVal));
    
    while (! isvalidInt(strVal))
    {
        return atoi(strVal.c_str());
    }

    return 0;
}// End readInt.

double readDouble(string prompt)
{
    string strVal("");
    
    do
    {
        cout << prompt;
        getline(cin, strVal);
    } while (! isvalidInt(strVal));
    
    while (! isvalidInt(strVal))
    {
        return atoi(strVal.c_str());
    }
    
   return 0;
}// End read double. 
Last edited on
Line 33,42,45: There is no need for the valid variable. If you detect that the string is not valid, you should simply return false; There is no need to continue further.

Line 44: You're only testing the if first character is not a digit. You need to iterate through all the postions in the string (except the sign if present) to test for digits.
I updated my post can you tell me how I would fix my do while and if else?
Which do/while? You have multiple do/whiles in your code.

Line 21: You're doing a cin to an integer. Lines 23,27: You're comparing that integer to quoted characters. That's not going to work. If you want to use I and D, then you need to change selection to a char.

As far as I can tell, you have ignored the advice I gave you in my previous post.
ok thanks I got the do/ while and the if else to work. Sorry if I seamed to ignore your correction I was not. I just updated the form page before I read what you said so the numbers all all off could you tell me the correct numbers ? Sorry!
Here's an updated isvalidInt:
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
bool isvalidInt(const string & str)
{   int start = 0;          //start position in the string
    
    //check for an empty string
    if (str.length() == 0) 
        return false;  // no point in checking further
    
    if (str[0] == '-' || str[0] == '+')
    {   start = 1;    
        if (str.length() == 1)
            return false;  // Not valid.  Don't continue
    }
    //  Iterate through the rest of the string checking for non-digits
    for (int i=start; i<str.length(); i++)
        if (! isdigit(str[i]))
	return false; 

    //  All checks passed
    return true;	
}
Last edited on
oh I see! ok well now my other functions that are using this function gives me an error that says "Call to 'isvalidInt' is ambiguous" I think this is because of const string & str But I don't know how to fix this problem.

And the last function is the double function but obviously it's not finished. I still need to make a loop that makes sure that there are ether 0 OR 1 decimals in the double int that is interred (can't have more then one decimal) I just don't know how to go about this.
Last edited on
I changed the signature of isvalidInt. The function prototype at line 9 needs to match.

(can't have more then one decimal) I just don't know how to go about this.

Have you tried counting the decimals?

I know that you have to count the decimals but I am not sure how to do that. I guess you would make a for loop and use count and then make sure from there how many decimals you have. Is that how you would do it?
Yes.
what should I put in line 99?


also: can you help we with the out put it does not tell me when I have entered a wrong int. were should I put this statement cout << "invalid int plz try agin\n";

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//  main.cpp
//  Program 11
//  Created by William Blake Harp on 8/6/14.

#include <iostream>

using namespace std;

bool isvalidInt(const string & str);
int readInt(string prompt);
double readDouble(string prompt);

int main()
{
    int number;
    char selection;
    
    do
    {
        cout << "Enter 'I' for integer or 'D' for double: " << endl;
        cin >> selection;
    
        if (selection == 'i' || selection == 'I')
        {
            number = readInt("Enter an int: ");
        }
        else if(selection == 'd' || selection == 'D')
        {
            number = readDouble("Enter a double int: ");
        }
        else
        {
            cout << "invalid selection please try again\n\n";
            
        }
    }while (selection != 'i' || selection != 'I' || selection != 'd' || selection != 'D');
        
    return 0;
}// End Main.

bool isvalidInt(const string & str)
{
    int start = 0;          //start position in the string
    bool sign = false;      //assume no sign
    
    //check for an empty string
    if (str.length() == 0)
        return false;  // no point in checking further
    
    if (str[0] == '-' || str[0] == '+')
    {   sign = true;
        start = 1;
        if (str.length() == 1)
            return false;  // Not valid.  Don't continue
    }
    //  Iterate through the rest of the string checking for non-digits
    for (int i=start; i<str.length(); i++)
        if (! isdigit(str[i]))
            return false;
    
    //  All checks passed
    return true;
}

int readInt(string prompt)
{
    string strVal("");
    
    do
    {
        cout << prompt;
        getline(cin, strVal);
    } while (! isvalidInt(strVal));
    
    while (! isvalidInt(strVal))
    {
        return atoi(strVal.c_str());
    }

    return 0;
}// End readInt.

double readDouble(string prompt)
{
    string strVal("");
    
    do
    {
        cout << prompt;
        getline(cin, strVal);
    } while (! isvalidInt(strVal));
    
    while (! isvalidInt(strVal))
    {
        return atoi(strVal.c_str());
    }
    for (int count = 0, ; count != 1; count++)
    {
        
    }
    
   return 0;
}// End read double. 
Line 75: What's the point of the while loop here? YOu have already determined the string is a valid integer.

Line 91: Your while condition is going to fail if you enter a decimal point since isvalidInt does not allow decimal points.

Line 93: Your while condition makes no sense here. If it's not a valid integer, what's the point of calling atoi. You should be calling atod for doubles.

Line 97-100: I'd change these lines as follows:
1
2
3
4
5
6
  int count = 0;
  for (int i=0; i<strVal.length(); i++)
    if (strVal[i] == '.')
      count++;
  if (count > 1) 
    // return some error condition 




Line 75: What's the point of the while loop here? YOu have already determined the string is a valid integer.


fixed that.

Line 91: Your while condition is going to fail if you enter a decimal point since isvalidInt does not allow decimal points.


so should I wake a "isvaliddouble" is that what your saying?

Line 93: Your while condition makes no sense here. If it's not a valid integer, what's the point of calling atoi. You should be calling atod for doubles.


Yes i see that show to fix this I first need to make "isvaliddouble".... right?
I first need to make "isvaliddouble".... right?

Yes.

Topic archived. No new replies allowed.