Problem with char and arguments passed by value

Hello,
I'm just started to learn the C++, and tried to make a simple program. In fact, I made a nice problem:
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
#include <iostream>
using namespace std;

void radius () {
	const double 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)
Last edited on
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)
Why on Earth are you using goto in your program?
@jsmith
Ah, i see. You where right about the radius() content. I think I better go back to the documentation... :) Thx

@PanGalactic
I used goto because i learned pascal before and goto "keyword" was something new to me.
Topic archived. No new replies allowed.