Switched to new IDE - program no longer runs correctly

Hi. I just installed code::blocks to replace dev-C++. I have a program that runs fine when compiled with dev-C++ but not with code::blocks. In particular, this section of my code seems to be causing the problem:

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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<sstream>
using namespace std;
int check1=0;
int main()
{
int Check(string a);
.
.
.   
MaxTemp=Check("maximum");
    cout<<MaxTemp;
.
.
.
}

int Check(string a) /*This function checks to see that a character input
consists only of digits and is a multiple of 50, and if so, returns the string
as an integer*/
{
    string b,c;
    int x;
    double y,z;
    cout<<"\nEnter the "<<a<<" temperature: ";
    stringstream ss;
    while(1==1)
    {
        getline(cin,b);
        for(i=0;i<b.size();i++)
        {
            if(isdigit(b[i]))
                check1=check1+1;
        }
        if(check1==b.size())
        {
            check1=0;
            ss<<b;
            ss>>y;
            cout<<b<<endl;
            z=y/50;
            ss.clear();
            ss<<z;
            ss>>c;
            cout<<c<<endl;
            ss.clear();
            for(i=0;i<c.size();i++)
            {
                if(c[i]=='.')
                    check1=check1+1;
            }
            if(check1==0)
            {
                ss<<b;
                ss>>x;
                cout<<x<<endl;
                return x;
                break;
            }
            else
                cout<<"\n                    Invalid Entry.\n\n"<<"Enter the "<<a<<" temperature: ";
        }
        else
            cout<<"\n                    Invalid Entry.\n\n"<<"Enter the "<<a<<" temperature: ";
        check1=0;
    }
} 


I included some cout commands to try to find out where the problem is occurring. Let's say the user types 650 as the temperature input. This is what the console window prints out:

650
13
650
10227456


The last number changes each time I run the program. I don't understand what's going on. The value x in the function is correct, but when I return it to the main function and assign it to MaxTemp, it changes. Please help.
There are some cases where you do not return a value, in which case the value returned will be garbage.
Could you explain these cases, and why this is one of them? Why would it work with dev-C++ but not code::blocks?
I don't know why the error. Looks like the while loop ends only when x is returned. You don't need the break after return - it is unreachable code.

How is MaxTemp declared?
As an integer.

EDIT: you were right, i first broke out of the loop, then return x outside the loop. Now it works fine. Odd. Not really sure why this was happening. Thanks for the help though!
Last edited on
It would probably be better if you seperated your string checking, prompting for input and actually getting the input.
Not sure what you mean. I prompt the user for input before the loop starts, then I include the getline in the loop. The loop continues infinitely and the user is continuously asked to enter a valid temperature until a correct temperature is entered, then the loop breaks and the function returns the temperature. Is that not the same as "prmpting for input and actually getting the input"? The program seems to be working fine now, but I'd still like to know what you mean, in case there is some hidden problem. Thanks. Also, can anyone tell me why the function returned a garbage value when I included the return inside of the loop?
Also, can anyone tell me why the function returned a garbage value when I included the return inside of the loop?

The return in the loop still worked fine for me using portable CodeBlocks EDU edition.

With your original code maybe see what is in MaxTemp before you assign Check's result to it.
If MaxTemp garbage is the same before and after then maybe the return value is OK but it is not being assigned to MaxTemp or something?

1
2
3
cout<<MaxTemp; // 
MaxTemp=Check("maximum");
    cout<<MaxTemp;


Topic archived. No new replies allowed.