Using Z to exit

Not sure what am doing wrong on this program, trying to use atoi to hit 'Z' to exit from the console:
<
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>


int _tmain(int argc, _TCHAR* argv[])//main program begins here.
{
double n;
char Z;
repeat: //to be reused over and over command

cout << "Please enter Richter Scale Number:"<<endl;
cin >> n;
char Z = atoi(n.c_str());

>
Last edited on
 
double Z = atoi(n.c_str());


A couple of problems with this line.
1) n is a double. doubles have no c_str() function.
2) Z is already defined.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/

Not sure if this looks better:
1
2
3
4
5
6
7
8
9
int _tmain(int argc, _TCHAR* argv[])//main program begins here.
{
	float n;
	char Z;
    repeat: //to be reused over and over command

	cout << "Please enter Richter Scale Number:"<<endl;
	cin >> n;
	float n = atoi(Z.c_str());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    string s;
    double n;

    while (true)
    {
        cout << "Please enter Richter Scale Number:"<<endl;
        cin >> s;
        if (s[0] == 'z' || s[0] == 'Z')
            break;
        n = atoi(s.c_str());
        cout << "number was " << n << endl;
    }

    cout << "done" << endl;
}
Here is the full program and it has failed. Please point me in the right direction.
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
#include "stdafx.h"
#include <iostream> // instructs the preprocessor to include a section of standard C++ code, known as header iostream
using namespace std;
#include <cstring>
#include <cstdlib>


int _tmain(int argc, _TCHAR* argv[])//main program begins here.
{
	double n;
	string Z= " ";
    repeat: //to be reused over and over command

	cout << "Please enter Richter Scale Number:"<<endl;
	cin >> n;
	n = atoi(Z.c_str());

		if (n < 5.0)
			cout << "Little or no damage!" << endl;
		else if (5.0 <= n && n < 5.5)
			cout << "Some damage!" << endl;
		else if (5.5 <= n && n < 6.5)
			cout << "Serious damage:Walls may crack or fall!" << endl;
		else if (6.5 <= n && n < 7.5)
			cout << "Disaster: Houses and Buildings may collapse!" << endl;
		else
			cout << "Catastrophe: Most buildings Destroyed!" << endl;

		cout << "To exit input Z" << endl;//incase the user wants to exit
		goto repeat; //repeat the sequence unless otherwise.
	
	system("PAUSE");
	return 0;
}
On line 15 I think you mean to read into Z, not n. Also you really should use the loop structure that Chervil used above rather than a goto statement. Your current program provides no way for the user to end it, even if they input 'Z'.
Last edited on
Also note that if the string doesn't contain a valid integer, atoi() will return 0 as the result, your code may need to account for that (difficult if zero is a valid value). You could get over that obstacle by using a stringstream instead of atoi.
Topic archived. No new replies allowed.