C++ loop not working

Dec 13, 2013 at 6:41am
I would like to have my program as a .exe file that is clickable and will run without closing quickly. I want it to where if I put an input in, it runs, then starts over at the beginning and doesn't close until I press '0'. How do I accomplish this?
--------------------------------------------------------------------------------
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
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
	double ftemp;
	double ctemp;
	double ktemp;
	double select = -1;

	cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
	while (select !=-1)
	
		cout << "Please select from the following (0 to quit): " <<endl;
		cout << "1) Fahrenheit-to-Celsius" << endl;
		cout << "2) Celsius-to-Fahrenheit" << endl;
		cout << "3) Kelvin-to-Celsius" << endl;
		cout << "4) Celcius-to-Kelvin" << endl;
		cout << "5) Kelvin-to-Fahrenheit" << endl;
		cout << "6) Fahrenheit-to-Kelvin" << endl;
		cout << "Enter: ";
		cin >> select;
		if (select == 1)
		{
			cout << "Enter temperature in Fahrenheit to convert to degrees Celsius: ";
			cin >> ftemp;
			ctemp = (ftemp-32) * 5 / 9;
			cout << "Equivalent in Celsius is: " << ctemp << endl ;
		}
		if (select ==2)
		{
			cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";
			cin >> ctemp;
			ftemp = ctemp*9/5 + 32;
			cout << "Equivalent in Fahrenheit is: " << ftemp << endl;
		}
		if (select ==3)
		{
			cout << "Enter temperature in Kelvin to convert to Celsius: ";
			cin >> ktemp;
			ctemp = (ktemp-273.15);
			cout << "Equivalent in Celsius is: " << ctemp << endl;
		}
		if (select ==4)
		{
			cout << "Enter Celsius to convert to Kelvin: ";
			cin >> ctemp;
			ktemp=ctemp+273.15;
			cout << "Equivalent in Kelvin is: " << ktemp << endl;
		}
		if
		
			(select ==5)
		{	cout << "Enter Kelvin to convert to Fahrenheit: ";
			cin >> ktemp;
			ftemp= (ktemp-459.67);
			cout << "Equivalent in Fahrenheit:" << ftemp << endl;
		}
		if
			(select ==6)
		{
			cout << "Enter Fahrenheit to convert to Kelvin: ";
			cin >> ftemp;
			ktemp= (ftemp+459.67);
			cout << "Equivalent in Kelvin:" << ktemp << endl;
		}
		


		if (select < 0 || select > 6) // invalid if either of these is true
		
		do {
			cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
		}
		while (select <0 || select > 6) {
cout << select << ",":
		--select;
		}
	return 0;
}
Dec 13, 2013 at 8:12am
1
2
3
4
5
6
7
8
9
unsigned  i = 0
do {
    //...
    if( i == 1 ) {}
    else if( i == 2 ) {}
    // ...
    else if( i > 6 ) {}

} while( i != 0 );


I would like to have my program as a .exe file that is clickable


That is an advance one, if you want you can take a look how to do so :
http://www.benryves.com/tutorials/winconsole/

But that's not recommended, you can use instead graphics lib like sfml
Dec 13, 2013 at 8:22am
I'm not making a game, I'm just trying to convert temperatures back and forth. Is there a way to make it runnable from a .exe file.
Dec 13, 2013 at 8:32am
First:
1
2
3
4
        double select = -1;

	cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
	while (select !=-1)


? Doesn't look right. Change that double to an int.

I guess you're using VS. You have a project folder, and there is an exe somewhere in there. You would be able to click on that to get your program to run.
Dec 14, 2013 at 2:33am
The reason why there are double's is because I would like a decimal set to 2 places. When I click on the .exe file and put in a value, it creates an output, then closes quickly, and I don't have time to see it.
Update to last comment: I didn't realize you meant that last double, my bad. I did change that 'double' to 'int,' but it still closes too quickly.
Last edited on Dec 14, 2013 at 4:12am
Dec 14, 2013 at 9:13am
Place a cin.ignore() at the end. That will do for now for stopping the thing closing. You can then hit return (or another key) to exit.
Last edited on Dec 14, 2013 at 9:13am
Dec 14, 2013 at 10:06am
It's not doing anything. Here's 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
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
	double ftemp;
	double ctemp;
	double ktemp;
	int select = -1;

	cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
	while (select !=-1)
	
		cout << "Please select from the following (0 to quit): " <<endl;
		cout << "1) Fahrenheit-to-Celsius" << endl;
		cout << "2) Celsius-to-Fahrenheit" << endl;
		cout << "3) Kelvin-to-Celsius" << endl;
		cout << "4) Celcius-to-Kelvin" << endl;
		cout << "5) Kelvin-to-Fahrenheit" << endl;
		cout << "6) Fahrenheit-to-Kelvin" << endl;
		cout << "Enter: ";
		cin >> select;
		if (select == 1)
		{
			cout << "Enter temperature in Fahrenheit to convert to degrees Celsius: ";
			cin >> ftemp;
			ctemp = (ftemp-32) * 5 / 9;
			cout << "Equivalent in Celsius is: " << ctemp << endl ;
		}
		if (select ==2)
		{
			cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";
			cin >> ctemp;
			ftemp = ctemp*9/5 + 32;
			cout << "Equivalent in Fahrenheit is: " << ftemp << endl;
		}
		if (select ==3)
		{
			cout << "Enter temperature in Kelvin to convert to Celsius: ";
			cin >> ktemp;
			ctemp = (ktemp-273.15);
			cout << "Equivalent in Celsius is: " << ctemp << endl;
		}
		if (select ==4)
		{
			cout << "Enter Celsius to convert to Kelvin: ";
			cin >> ctemp;
			ktemp=ctemp+273.15;
			cout << "Equivalent in Kelvin is: " << ktemp << endl;
		}
		if
		
			(select ==5)
		{	cout << "Enter Kelvin to convert to Fahrenheit: ";
			cin >> ktemp;
			ftemp= (ktemp-459.67);
			cout << "Equivalent in Fahrenheit:" << ftemp << endl;
		}
		if
			(select ==6)
		{
			cout << "Enter Fahrenheit to convert to Kelvin: ";
			cin >> ftemp;
			ktemp= (ftemp+459.67);
			cout << "Equivalent in Kelvin:" << ktemp << endl;
		}
		


		if (select < 0 || select > 6) // invalid if either of these is true
		
		do {
			cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
		}
		
		
		
	return 0;
cin.ignore()}

I've tried moving the cin.ignore() around at the bottom of the code, and no matter where I put it, it doesn't work. Before and after the:
1
2
3
4
5
                if (select < 0 || select > 6) // invalid if either of these is true
		
		do {
			cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
		}
Last edited on Dec 14, 2013 at 10:09am
Dec 14, 2013 at 10:52am
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
#include <iostream>
using namespace std;

int main()
{
    double ftemp;
    double ctemp;
    double ktemp;
    int select = -1; // int ! not double
    
    while (select != 0)
    {
        cout << "Please select from the following (0 to quit): " <<endl;
        cout << "1) Fahrenheit-to-Celsius" << endl;
        cout << "2) Celsius-to-Fahrenheit" << endl;
        cout << "3) Kelvin-to-Celsius" << endl;
        cout << "4) Celcius-to-Kelvin" << endl;
        cout << "5) Kelvin-to-Fahrenheit" << endl;
        cout << "6) Fahrenheit-to-Kelvin" << endl;
        cout << "Enter: ";
        cin >> select;
        
        if (select == 1)
        {
            // ...
        }
        else if (select ==2)
        {
            // ...
        }
        else if (select ==3)
        {
            // ...
        }
        else if (select ==4)
        {
            // ...
        }
        else if (select ==5)
        {
            // ...
        }
        else if (select ==6)
        {
            // ...
        }
        else if (select < 0 || select > 6)
        {
            do
            {
                cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
                cin >> select;
            }
            while( select < 0 || select > 6 );
        }
    }

    cin.get(); // wait for a keypress
    return 0;
}
Last edited on Dec 14, 2013 at 10:53am
Dec 14, 2013 at 12:42pm
Does this look right? I've tried building it, and the .exe file still closes too quickly.
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
#include <iostream>
using namespace std;

int main()
{
    double ftemp;
    double ctemp;
    double ktemp;
    int select = -1; // int ! not double
    
    while (select != 0)
    {
        cout << "Please select from the following (0 to quit): " <<endl;
        cout << "1) Fahrenheit-to-Celsius" << endl;
        cout << "2) Celsius-to-Fahrenheit" << endl;
        cout << "3) Kelvin-to-Celsius" << endl;
        cout << "4) Celcius-to-Kelvin" << endl;
        cout << "5) Kelvin-to-Fahrenheit" << endl;
        cout << "6) Fahrenheit-to-Kelvin" << endl;
        cout << "Enter: ";
        cin >> select;

		if (select == 1)
		{
			cout << "Enter temperature in Fahrenheit to convert to degrees Celsius: ";
			cin >> ftemp;
			ctemp = (ftemp-32) * 5 / 9;
			cout << "Equivalent in Celsius is: " << ctemp << endl ;
		}
		else if (select ==2)
		{
			cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";
			cin >> ctemp;
			ftemp = ctemp*9/5 + 32;
			cout << "Equivalent in Fahrenheit is: " << ftemp << endl;
		}
		else if (select ==3)
		{
			cout << "Enter temperature in Kelvin to convert to Celsius: ";
			cin >> ktemp;
			ctemp = (ktemp-273.15);
			cout << "Equivalent in Celsius is: " << ctemp << endl;
		}
		else if (select ==4)
		{
			cout << "Enter Celsius to convert to Kelvin: ";
			cin >> ctemp;
			ktemp=ctemp+273.15;
			cout << "Equivalent in Kelvin is: " << ktemp << endl;
		}
		else if (select ==5)
		{	cout << "Enter Kelvin to convert to Fahrenheit: ";
			cin >> ktemp;
			ftemp= (ktemp-459.67);
			cout << "Equivalent in Fahrenheit:" << ftemp << endl;
		}
		else if (select ==6)
		{
			cout << "Enter Fahrenheit to convert to Kelvin: ";
			cin >> ftemp;
			ktemp= (ftemp+459.67);
			cout << "Equivalent in Kelvin:" << ktemp << endl;
		}
		else if (select < 0 || select > 6) // invalid if either of these is true
		{
            do
            {
                cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
                cin >> select;
            }
            while( select < 0 || select > 6 );
       			}
	}
	cin.get(); // wait for a keypress
    return 0;
}
Dec 14, 2013 at 6:58pm
Just put cin.ignore(2) there is probably a '\n' left over.
Dec 15, 2013 at 2:31am
That didn't work either. I ran 'calculate code metrics for solution' under 'analyze' and it stated "Project: ConsoleApplication14
Configuration: Debug
Scope: None
Assembly: C:\Users\bill\Documents\Visual Studio 2012\Projects\ConsoleApplication14\Debug\ConsoleApplication14.exe
Maintainability Index:
Cyclomatic Complexity:
Depth of Inheritance:
Class Coupling:
Lines of Code:
Message: The project target file 'C:\Users\bill\Documents\Visual Studio 2012\Projects\ConsoleApplication14\Debug\ConsoleApplication14.exe' contains no managed code."
What does this mean?
Topic archived. No new replies allowed.