-
quick ? people
aight im doing an exercise in a book and i want it to ask how many total points can the student earn which i can do that but i i want it to say like press 'q' to quite and obviously points are numbers and thats crossing variable types so is there a way i can make so i can still ask for a number and use it for a number while allowing a 'q' for input i tryed jus flat out using 'q' there but it jus repeated some output and did nuthin so can anyone help out real fast thanx bye
-
take it easy take your time to write a post(if u want uss to do the same)
-
disclaimer: i have almost no idea what you mean.
read everything in as a string, test for 'q' or 'Q', then convert to int or float and proceed
-
Input everything as a string, then decide how to convert it by looking at the chars typed in
-
um two variables of int and char
get the number
ask if they want to do it again (y/n)
get char
repeat if y.
-
make a big loop
use swich as after the () of the main
int main() {
cout inter the first char
switch {char x of
case 'c' : continure {
youwrite here your progam
}
case' q': break;
}// switch
cout <<" Good by"
}/main
-
oh so i ask seperately if they want to continue or not? i cant make it all one question?
-
> oh so i ask seperately if they want to continue or not? i cant make it all one question?
Of course you can
Code:
char answer[100];
cin.getline( answer, sizeof(answer) );
if ( isdigit( answer[0] ) {
// convert answer to a number
} else
if ( answer[0] == 'y' ) {
// yes
} else
if ( answer[0] == 'n' ) {
// no
}
Or any variation on this theme which you care to imagine
-
hmm while that makes sense except i havent learned what the isdigit() does exactly my guess is it converts the numberal naswer to digits or gets the numeric value for strings?
-
Nope. Try putting a question mark after the function name for a clue.
-
aight heres what i got
without the ? mark i have an error saying isdigit is an undeclared identifyer so is there a header that i need for this thing?
i think im getting the hint for the isdigit so it would ask with a ? sumwhere which i cant figure out quite where to put since i keep getting errors and i dont understand them since ive never known what ? marks do in coding altho i have seen them but heres what i got
i also tryed like 5 places for the ? mark jus to see what it did and i kept getting these 4 errors or errors similar to them in all cases
#include <iostream.h>
int main()
{
char answer[100];
cin.getline( answer, sizeof(answer) );
if ( isdigit?( answer[0] )) {
// convert answer to a number
} else
if ( answer[0] == 'y' ) {
cout<<"this is the yes slot"<<endl;
// yes
} else
if ( answer[0] == 'n' ) {
cout<<"this is the no slot"<<endl; // no
}
return 0;
}
--------------------Configuration: isdigit - Win32 Debug--------------------
Compiling...
isdigit.cpp
C:\Windows\Desktop\c++\isdigit.cpp(6) : error C2065: 'isdigit' : undeclared identifier
C:\Windows\Desktop\c++\isdigit.cpp(6) : error C2059: syntax error : ','
C:\Windows\Desktop\c++\isdigit.cpp(6) : error C2143: syntax error : missing ';' before '{'
C:\Windows\Desktop\c++\isdigit.cpp(8) : error C2181: illegal else without matching if
Error executing cl.exe.
isdigit.obj - 4 error(s), 0 warning(s)
-
include the string header file or ctype for 'isdigit'
-
I dont think Sorensen meant to literally put a '?' after isdigit() in your code. He meant it as a response to your question as to what is the purpose of isdigit(). Think about it a little. Or even better, use one of those bizarre things called 'search engines.' I've heard that the one called 'www.google.com' is pretty good. Kinda high-tech though. Alternatively, it is included in the functions section of this site.