Odd Quirk in the Code (Tested Multiple Times)

Mar 27, 2016 at 12:32am
I have just started to code in c++ this year, so I am by no means an expert. For an assignment from my teacher I was given the assignment to create a Safe Data Entry program that would (using some given code, and ours added) force a user to enter a proper value (whether double or integral). I created the code for this and most of it seemed to make logical sense to me and my associates the only issue is, it doesn't work. We have checked it over (More than two people), and have found nothing wrong. Code is here in a google doc, so I could post it quicker.

https://docs.google.com/document/d/1-_tMMpNoSBZssK0VCrWWLj20S0aIV-_5yOPFpKqykxM/edit?usp=sharing

-Thanks
Mar 27, 2016 at 12:57am
At a glance, you used = where you should have used ==. Also, the do-while condition in the getDouble function can never be true. Is there a reason you're testing first character of the strings for every index into the string (and why is that part of an if/else chain?)

The code at the link given above:
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;

int getInt( void );        //returns viable int
double getDouble( void );    //returns viable double

int main() 
{
    int intTest = -9999;
    double doubleTest = -999.999;

    cout << "  Welcome to Safe Data Entry!\n===============================" << endl;
    
    intTest = getInt();
    cout << "You have successfully entered an integer, " << intTest << endl;
    cout << intTest << " + 5 = " << intTest + 5 << endl;  //proves an int
    /*
    doubleTest = getDouble();
    cout << "You have successfully entered an double " << doubleTest << endl;
    cout << doubleTest << " + 10.123 = " << doubleTest + 10.123 << endl;  
    */
    cout << endl;
    system("pause");
    return EXIT_SUCCESS; 
}

/*
int getInt(void) gets data from the user, stores it in a string
tests the string to ensure the value entered is an int(positive or negative),
then converts the string to an int to be returned to the main().
*/
int getInt( void )
{
    string str = "";    int a = 1, d = 1;
    while(a = 1)
    {
        a = 0;
        cout << "Please enter a Integer   ";
        cin >> str;
        cout << endl;
        for(int i = 0; i < str.size(); i++)
        {
            if(isdigit(str[i]))
            {
                //no effect
            }
            else if(str[0] == '-')
            {
                //no effect
            }
            else
            {
                a = 1;
            }
        }
    }
    int b = atoi( str.c_str() );
    return b;
}

/*
double getDouble(void) gets data from the user, stores it in a string
tests the string to ensure that the value entered is a double ( maybe even
checking for only 0 or 1 decimals and positive or negative),then converts the 
string to an double to be returned to the main().
*/
double getDouble( void )
{
    string str, str2;    int a = 1, b = 0;
    do{
        cout << "Please enter a Double   ";
        cin >> str;
        cout << endl << endl;
        for(int i = 0; i < str.size(); i++)
        {
            if(str[0] == '-')
            {
                a = a*-1;
            }
            else if(isdigit(str[i]))
            {
                str2 += str[i];
            }
        }



    }while(a == 0);
    return 0; 
}
Topic archived. No new replies allowed.