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());
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/
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());
#include "stdafx.h"
#include <iostream> // instructs the preprocessor to include a section of standard C++ code, known as header iostream
usingnamespace 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;
elseif (5.0 <= n && n < 5.5)
cout << "Some damage!" << endl;
elseif (5.5 <= n && n < 6.5)
cout << "Serious damage:Walls may crack or fall!" << endl;
elseif (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'.
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.