First C++ programming assignment, divide for answer with remainder

My assignment is to write a program that will divide two numbers and give an answer with a remainder. I was given a faulty code that I need to fix. Things that I have edited have a // comment next to them. Here is what I've got so far:

// remainder.cpp
// This program contains several errors; make a note of your corrections and execute the program

#include <iostream> // added <> brackets around iostream
using namespace std;

int main(); // added parentheses and semicolon
int firstNumber; // changed comma to semicolon
double secondNumber;
{
cout << "Please enter two whole numbers: " << endl; // changed >> to << and added << endl;
cin >> firstNumber >> secondNumber >> endl; // added >> endl;
cout << firstNumber << " divided by " << secondNumber;
<< " is " << (firstNumber / secondNumber) << endl; // added semicolon
<< "with a remainder of " << (firstNumber % secondNumber)
<< endl;
} // added bracket

But when I compile I get an error that says "error C2447: '{' : missing function header (old-style formal list?)" I've googled what that means and I found that it generally means I have an extra { bracket, but I only have two in my code and I'm fairly sure they are correct. Please help?
The semicolon after main() shouldn't be there.
Put the variable declarations inside the function body.
Please use [code] tags, like this:

[code]
#include <iostream>
using namespace std;

int main()
{
  const char* greeting = "Hello world!";
  cout << greeting << endl;
  return 0;
}
[/code]

to produce this:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
  const char* greeting = "Hello world!";
  cout << greeting << endl;
  return 0;
}


As to your questions:

1: "missing function header (old-style formal list?)"
Your main() function looks wrong. See the example I have given you. Where should the variables 'firstNumber' and 'secondNumber' be declared?

2: Remember that semicolons only end a statement. Make sure you don't add them in the middle of your 'cout << x << y << z' statements.

Hope this helps.
Like this?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// remainder.cpp
// This program contains several errors; make a note of your corrections and execute the program  

#include <iostream> // added <> brackets around iostream
using namespace std;

int main() // added parentheses and semicolon
{
    int firstNumber; // changed comma to semicolon
	double secondNumber;
	cout << "Please enter two whole numbers: " << endl; // changed >> to << and added << endl;
    cin >> firstNumber >> secondNumber >> endl; // added >> endl;
    cout << firstNumber << " divided by " << secondNumber;
         << " is " << (firstNumber / secondNumber) << endl; // added semicolon
         << "with a remainder of " << (firstNumber % secondNumber)
         << endl;
} // added bracket 


I went from one error to three errors. One of the errors is really long and the other two are "error C2143: syntax error : missing ';' before '<<'"

I'm taking this course online so I'm basically teaching myself and it is really not easy so any help is greatly appreciated.
Last edited on
You can't >> std::endl. std::endl is purely for output.
So every line that starts with cin I have to get rid of endl;?

What does std mean?
Lines 13 and 14 have semicolons in the middle of your 'cout' statement.

You are doing well.
Is that sarcasm? Because I feel like I have absolutely no idea what I'm doing.

Here is what I've got now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// remainder.cpp
// This program contains several errors; make a note of your corrections and execute the program  

#include <iostream> // added <> brackets around iostream
using namespace std;

int main() // added parentheses and semicolon
{
    int firstNumber; // changed comma to semicolon
	double secondNumber;
	cout << "Please enter two whole numbers: " << endl; // changed >> to << and added << endl;
    cin >> firstNumber >> secondNumber;
    cout << firstNumber << " divided by " << secondNumber
         << " is " << (firstNumber / secondNumber) << endl
         << "with a remainder of " << (firstNumber % secondNumber)
         << endl;
} // added bracket 


I've only got one error now.

error C2297: '%' : illegal, right operand has type 'double'

What does this mean and how can I fix it? Would I change double to float?

Edit: float didn't work..

Edit2: changed float to int and it worked!! Now I just hope it will execute properly..

Edit3: hmm.. the program ran and it asked me for two whole numbers, so I entered 12 and 6 and the window closed.. what does that mean?
Last edited on
My program is working but after I type in my two whole numbers it will do the calculation but will only show the result for a split second before the window closes. How do I make the window stay open?
It is not sarcasm.

Keep the console open long enough to see your program's output
http://www.cplusplus.com/forum/articles/7312/

:-)
Oh, well thank you for the compliment, and thank you all so much for the help. I've got it running properly now. I've got another code to write so I'll probably be hanging around here for a while, so if you all don't mind keep an eye on this thread.. I'm probably going to need more help.. Thanks again!
Topic archived. No new replies allowed.