#include <iostream>
usingnamespace std;
void radius () {
constdouble pi=3.141592654;
double r;
cout << "Radius: "; cin >> r;
cout << "A kor kerulete: " << 2*r*pi << endl;
cout << "A kor terulete: " << r*r*pi << endl;
cout << "A gomb felszine: " << 4*r*r*pi << endl;
cout << "A gomb terfogata: " << (4*r*r*r*pi)/3 << endl;
}
void ynexit (char& a){
cout << "Tipe y to restart, n to exit -> ";
cin >> a;
}
int main () {
char a;
myprog1:
radius ();
myprog2:
ynexit (a);
switch (a) {
case'y': goto myprog1; break;
case'n': return 0;
default: {cout << "\nTry again.\n"; goto myprog2;} }
}
So the funniest problem is when the program gets to the switch statemants and if the char a contains more than 1 character like yy it go mad and somewhere comes in an infinite loop.
Anyway the problem to resolve is: how can I limit char to contain only 1 character. If the answer is something like char a[1], than how can I bring it out from that void function? Caz when I tried it like above the VC++ Compiler hates me (lot of compile error).
Thanks for the answer, and sorry for my bad English and for goto function.
EDIT: void radius(). (There are some mathematical expression in sequence of characters witch I can't translate to English)
The implementation of radius() is important, because I strongly suspect you
are reading either an integer or a floating point number via cin there.
cin >> a;
reads only a single character from the input stream. If the input stream is
yy<ENTER>
then the first 'y' is read and the second one is left in the stream. When radius()
attempts to read a number, it sees the second 'y' in the input stream and balks
because 'y' is not a valid number.
There are plenty of examples/posts around here of how to properly read data
from input. I suggest searching for one. Typically the best answer is to use
std::getline() and parse the input yourself rather than using operator>>() on
cin.
There are also many lengthy discussions around here regarding use of goto, which
is a keyword, not a function. I suggest reading them as well before asking about
the pros and cons of its use. (I do not want to start yet another "discussion" on
the topic)