#include <iostream>
using namespace std;

int main()
{
	char a = 0, b = 1, ch1, ch2, ch3, ch4, chsum;
	float num1, num2, num3, sum, avg;

	cout<<"Enter Character 'a' or 'b': "; //asking user to enter character a or b
	cin>>ch1;
	
	if (ch1 == a) 
	{
		cout<<"Enter three floating-point numbers: " << endl;
		cout<<"First number: ";
		cin>>num1;
		cout<<"Second number: ";
		cin>>num2;
		cout<<"Third number: ";
		cin>>num3;
		sum = num1+num2+num3;
		avg = (num1+num2+num3)/3;
		cout<<"The sum of your numbers is: " << sum << endl;
		cout<<"The average of your numbers is: " << avg << endl;
	}
 	else if (ch1 == b)
	{
		cout<<"Enter the intials of your first, middle, and last name: " << endl;
		cout<<"First initial: ";
		cin>>ch2;
		cout<<"Second initial: ";
		cin>>ch3;
		cout<<"Third initial: ";
		cin>>ch4;
		chsum = ch2+ch3+ch4;
		cout<<"Your complete initials are: "<< chsum << endl;
	}

	system("pause");
}

When executed, the program does ask for the user to enter a character 'a' or 'b', but after that it goes no where....where am i going wrong?

Recommended Answers

All 6 Replies

You need to put single quotes around the letters

Change if (ch1 == a) to if (ch1 == 'a') You should also return 0; at the end of main(). You should also have an else statement to help catch things like this :)

Hope that helps,

David

That was embarrassing. Thank you so much works perfectly.

Beware of the code

chsum = ch2+ch3+ch4;

There may be buffer overflow due to the limitation of data type char which can have maximum value of 255 only.

You need to put single quotes around the letters

You should also return 0; at the end of main(). You should also have an else statement to help catch things like this :)

Hope that helps,

David

You absolutely should not.
The current C++ standard requires that the return type of the main() function be int, however it does not require main() to ACTUALLY return a value. The standard says, that if control reaches the end of main and encounters no return statement, it is equivalent to return 0. So, you don't have to write that

So there is no reason to "absolutely not", right - you're saying it's not wrong, just maybe not necessary? I would imagine many compiler will still complain that main doesn't return a value - but maybe I am wrong?

Sorry, maybe that's my bad English
by saying you absolutely should not I mean that absolutely refers to should rather that not, meaning you don't have to. Of course it is just not necessary, not wrong. As for many compilers complaining about that... well, I am not aware of any reasonably compliant compiler (which for me is equivalent to good compiler) which complains about main not returning a value. On the other hand, unfortunatly SOME compilers still accept void main() which is absolutely ill-formed.
Again, sorry for the misunderstanding

commented: Thanks for the explanation! +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.