Hello, i have been messing around with C++ for about a 5 weeks. In linux I have been using gcc or g++ i forgot what it was called. In windows i use DevC++. but I am in windows now due to ease of use with programing. So here is my code so far, i seem to be stuck because i want to do more with it and i dont know were to start
#include <windows.h>
#include <iostream>
usingnamespace std;
void odd (int a);
void even (int a);
int main ()
{
system("TITLE evenNodd");
system("COLOR 2");
system("PAUSE");
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n \a";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n \a";
else odd (a);
}
Because i am using a .int it can not go that high. it will just crash. also when i enter a letter or anything besides a number it crashes. I want it to say something like "silly goose you cant do that" or "This number is beyond my knowledge"
If you have any comments of my code or anything on the lines of this please get back to me.
Thanks man. i was a little confused on the link you sent me. i would this it would be an if statement so like if the number is higher then X then cout "too high". and i dont know how it would go for the letters. I am not very good with math (failing ninth grade math)
Using cin.getline(); is the easiest way to read a line. From there you can try to convert it to a number. If this fails you can handle the error and display the appropriate result.
Ok i know know how to repeat a cin into a cout. but how would i say if the number that is inputted is too high like or is a letter for this matter. thanks for the example.
#include <windows.h>
#include <iostream>
usingnamespace std;
void odd (int a);
void even (int a);
int main ()
{
system("TITLE evenNodd");
system("COLOR 2");
system("PAUSE");
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n \a";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n \a";
else odd (a);
}
void >100000 (int a)
{
if ((a%2)==0) cout << "The number is too high. \n \a"else > 100000 (a)
}
I hope by seeing what i am trying to do you can understand better.
i got an error on the compiling saying 33 C:\Documents and Settings\David\Desktop\oddNeven V.01.cpp expected unqualified-id before '>' token
Ok. i added the section of code and it compiled fine. but when I enter a number over 100K it gives the right answer. I tryed this out of pure guessing i know i am missing something.
after that i just got a munch of compiling errors so i just deleted it. im looking through one of my C++ books it is the sams teach your self C++ in 21 days
Yeah, I kind of an getting ahead of my self. i just got a book so ill take a sneak peak at that and just re read everything. this is my code now. i added a few things. i am calling this V .3 i dont know what to come but it can only get better.
#include <windows.h>
#include <iostream>
usingnamespace std;
void odd (int a);
void even (int a);
int main ()
{
system("TITLE evenNodd");
system("COLOR 2");
system("PAUSE");
system("CLS");
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n \a";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n \a";
else odd (a);
}
It is better now. i like it a lot. i have another question. if you input the number 0 it closes the program. how would i have something like a "Are you sure you want to exit? (Y or N)"
1) use #include <iostream> not <iostream.h. The .h version is depreciated now.
Your code has the potential to be an infinite loop.
Example:
You call odd (line 17) and it is not an odd number so it calls even (Line 25) and the user has entered junk input, so it's not even either, so it calls odd again (line 31). Thus your program would loop forever.
int main() {
int numberToTest = 0;
char input[128] = {0};
do {
cout << "Type a number (0 to exit): ";
cin.getline(input, 128);
numberToTest = atoi(input);
if (!odd(numberToTest))
cout << "Number if even" << endl;
} while (numberToTest != input);
}
Then code up your Odd function to return a boolean etc