Would somebody chck this code please. I am a struggling C++ student and need all the help I can get. Thanks, Curt

Here are the directions:
create a function that check's the number, put's out whether
it's odd or even and adds it to the total variable

#include"stdafx.h"
#include<iostream>
 
usingnamespace std;
//declare the variable's
int total;
int number_buffer = 1;
 
bool checker(int number)
{
if(number == 0){return false;}
if(number%2)
{
cout << number << " is odd" << endl;
} else {
cout << number << " is even" << endl;
}
total += number;
}
int main()
{
//request user input
cin >> number_buffer;
//continue to ask input until 0 is entered, 
//if 0 is entered does checker return's false, else true.
while(checker(number_buffer)){cin >> number_buffer;}
//show the total
cout << "Total: " << total;
return 0;
}

Would somebody chck this code please.

Why don't you use the compiler to check the code. Do you have question?
check.cpp: In function `bool checker(int)':
check.cpp:19: warning: control reaches end of non-void function
Thats after my compiler checking.

we have not covered the bool functions in class yet. This was something that was suggested to me, I think there is a way to use the function " counter" but am not sure. Any ideas would help. It compiles fine. I'm just looking for a different way of coding it.

I found that there are a little mistake that you need to do correction. usingnamespace std; should be written like this using namespace std; and you need to add a return true; statement after total += number; otherwise your coding for the true part will not execute properly or having error during compilation.

Just a few comments, since andor has already pointed out what appears to be the main problem.

usingnamespace std;

typo above, was that a result of colouring in the code? or didn't you copy&paste your code? as a general rule of thumb, never attempt to retype code, because typo's happen - making it hard to give helpful advice - paste it instead.

You've got the variables total and number_buffer declared global - you only need them inside your main() function - you can pass them around to functions if you need to (which you have done), so there's no need for them to be in the global scope. Using global variables is a bad habit, so good design avoids them whenever possible.

The header "stdafx.h" is unnecessary - something which MSVC++ uses when you have precompiled headers enabled. there are loads of hits on google which tell you how to disable precompiled headers.

Maybe something likie this is what you are looking for:

#include <iostream>
using namespace std ;

bool checker (int number, int& total)
{
    if (number == 0)
    {
        return false;
    }
    else
    {
        if (number%2 == 1)
            cout << number << " is odd" << endl;
        else
            cout << number << " is even" << endl;
        total += number;
        return true ;
    }
}

int main (void)
{

    int number_buffer = 0, total = 0 ;
    cin >> number_buffer;

    while ( checker (number_buffer, total) )
        cin >> number_buffer ;

    cout << "Total: " << total;
    return 0;
}

My output:

1
1 is odd
2
2 is even
3
3 is odd
4
4 is even
5
5 is odd
0
Total: 15
Press ENTER to continue.

Hope it helped, bye.

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.