Need help with a program...

Hey everybody. I've written this program and cannot figure out why it won't work. Here are my requirements...

A teacher wants a program which will test students’ memory of the values of small integers squared. She asks for a program which prompts the students for an integer. It then asks them for that number’s square. If their answer is correct they are congratulated if it is incorrect the correct value is given.

Because she wants the class to practice with this she asks that the program continue to prompt the students until they enter the integer: 0. The program will then thank the student and terminate.

INPUT: Your program should accept the value of an integer to square. If the integer entered is zero (0) the program terminates. If the number is other than zero (0) your program should then accept the student’s answer for the square of the integer accepted above.

OUTPUT: Either an error message or a congratulatory message should be output after every answer given.

Here's what I have...

#include <iostream>
#include <string>

int main()
{
string Err_Msg = "That is the wrong answer\n\n";
string Yes_Msg = "That\'s correct! Try again \t";
int num=0, ans=0;

cout << "Please input an integer to square:\t";
cin >> num;
cout << endl << endl;

cout << "What is the square of that number \t";
cin >> ans;
cout << endl << endl;

if (ans == pow(num,2)) cout << Yes_Msg;
else cout << Err_Msg;

return 0;
}



Answer this question

Need help with a program...

  • ravievg

    Math.h is needed for using pow hence my advise to you to try everything yourself so that we can go through resolving such issues and learning from them.

    Hint: use a while loop.

    Thanks,
    Ayman Shoukry
    VC++ Team


  • topgun2710

    Should the looping be done with a while loop
  • Vladimir Sapronov

    Here is something that should put you on track:

    #include <iostream>
    #include <string>
    #include <math.h>

    using namespace std;

    int main()
    {
    string Err_Msg = "That is the wrong answer\n\n";
    string Yes_Msg = "That\'s correct! Try again \t";
    int num=0, ans=0;

    cout << "Please input an integer to square:\t";
    cin >> num;
    cout << endl << endl;

    cout << "What is the square of that number \t";
    cin >> ans;
    cout << endl << endl;

    if (ans == pow(num,2.0)) cout << Yes_Msg;
    else cout << Err_Msg;

    return 0;
    }
    This will get past compilation errors and running your application. You need to figure out how to keep looping till you read a 0.

    Thanks,
    Ayman Shoukry
    VC++ Team


  • Larry Joy

    Also, in the code you wrote back, what does math.h do
  • Ganeshgoody

    It is for you to decide and depends on how much you covered in your course.

    I wouldn't really encourage you to seek the forums for answering homework issues!

    Thanks,
    Ayman Shoukry
    VC++ Team


  • JeremyRooks

    Thanks Jonathan...I appreciate your advise.
  • pubs

    It provides the compiler with the declaration of the pow function.

    Instead of asking such basic questions on this forum I would suggest getting a good book and reading up on C++ and/or C. There is a topic on recommended books at the start of this forum.



  • spardo

    Thanks, Ayman. I don't see a problem with what I am doing because my professor encourages us to seek outside support. Plus, I'm not solely relying on outsiders to do the work for me. As you saw I wrote what I could with the program and asked for pointers. Again, thank you for your help.


  • Need help with a program...