*** LESSON 1 *** communication
Your code...
Code:
int input, rem, evensum = 0, oddsum = 0, negsum = 0, total = 0, possum = 0;
double per, avg;
... doesn't say anything to me. You will need to rename your variables (and functions later and everything else even later) so that a stranger like me does know what you mean.
We start simple:
- We need an
input variable. That's one thing that is certain. We want to input
integers so it seems. WRONG.
The real requirements are: we want to ACCEPT only integers.
OUR JOB:
Set up some basic code that accepts integers, and integers only.
YOUR JOB (I will do this one for you as an example!):
Make a loop that accepts
anything even if wrong input causes errors.
MY SOLUTION:
Code:
#include <iostream>
#include <string>
using namespace std;
//Optional functions: depends on IDE. void wait_for_enter();
int main()
{
int input;
while (1)
{
cout << "Enter an integer(CTRL-Z/D to exit) -> ";
cin >> input;
}
wait_for_enter();
return 0;
}
//////////////////////////////////////////////// void wait_for_enter()
{
cout << "press <enter> to continue...\n";
// Reset failstate, just in case.
cin.clear();
string line;
getline( cin, line);
}
//////////////////////////////////////////////// Congratulations, your first assignment is done. We move on now.
YOUR 2nd JOB:
Experiment with it.
- What happens if I enter integers (neg and pos)?
- What happens if I enter "j" instead of integers?
- What happens if I enter CTRL-Z or D for that matter?
- What happens if I Enter 2.5 (fractions)?
- How could I test this? HINT: outcomment the things you don't need, so you'll have some output.
Do it, post asap when done.