alright, i'm just learning c++
and i've put a code in to find the square of a number
this is the code i used;

#include<iostream>
void main()
{
char cReply;
int iNum, iSquare;
std::cout << "Do you want to find the square of a number (y/n)?";
std::cin >> cReply;
while(cReply == 'y')
{
std::cout << "Enter a number:";
std::cin >> iNum;
iSquare = iNum * iNum;
std::cout << "The square of "<<iNum<<"is"<<iSquare<<std::endl;
std::cout << "Do you want to find the square of another number (y/n)?";
std::cin >> cReply;
}
}

but it fails everytime

help?

Recommended Answers

All 4 Replies

In C++ main has to return an int (or possibly some kind of comprehensible value), not void.

#include<iostream>
int main()
{
char cReply;
int iNum, iSquare;
std::cout << "Do you want to find the square of a number (y/n)?";
std::cin >> cReply;
while(cReply == 'y')
{
std::cout << "Enter a number:";
std::cin >> iNum;
iSquare = iNum * iNum;
std::cout << "The square of "<<iNum<<"is"<<iSquare<<std::endl;
std::cout << "Do you want to find the square of another number (y/n)?";
std::cin >> cReply;
}

return 0;
}

It looks fine to me.
Unless you the number you want its square is a floating point then you should be using float instead of an integer.

If the compiler is giving an error message then post it here 'cause the code works fine.

Or maybe you shout try adding this function before every cin statement:
fflush(stdin);

This will tae care of in reaming input from the standard input.

If i'm saying something wrong I hope someone will correct me.

Or maybe you shout try adding this function before every cin statement:
fflush(stdin);

This will tae care of in reaming input from the standard input.

If i'm saying something wrong I hope someone will correct me.

Don't do that, see here
http://www.gidnetwork.com/b-57.html

i just needed to replace "void" with "int"
thanks a lot :)

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.