oddNeven program having trouble adding on.

Pages: 12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <windows.h>
#include <iostream>
using namespace 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.


David D.
Last edited on
Uh........ can someone move this to the beginner section for me?
Don't fret.

Have a look through:
http://www.cplusplus.com/forum/beginner/5000/page2.html
And link I posted on it.
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.

No math required.
How would i use that? the cin.getline()
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.

I tried this by just guessing and as you can see it did not work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <windows.h>
#include <iostream>
using namespace 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

And then it highlights the
void >100000 (int a)
That's not a valid function number. Function names must start with either an _ or a letter. You cannot use < > = + etc in a function name.
Yeah i guessed that. so how would i adress a function over the number 100000. and any letter. or is a letter even a function?
void above100k() {

}
Thanks a lot.
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.
1
2
3
4
void above100k() {
   if above100k  cout << "too high. \n \a";

}


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
Last edited on
Your logic is flawed in your design.

1
2
3
4
5
6
7
8
int value = 3;

if (value > 100000)
 cout << "Value is too high" << endl;
else if ( (value%2) == 0)
 cout << "Value is even" << endl;
else
 cout << "Value is false" << endl;


To use a function, You'd want something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isEven(int value) {
 return ((value%2) == 0)

}
int main() {

 int value = 3;

 if (isEven(3))
  cout << "Value is even" << endl;
 else
  cout << "Value is odd" << endl;

 return 0;
}


You need to get your if-statements correct before trying to use functions. Spend some more time with basic logic before introducing extra components.



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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <windows.h>
#include <iostream>
using namespace 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)"
Last edited on
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.

Line 31, is not required.
Yes that is what I mean when i say crash. it just keeps saying the number is odd like for ever.
Try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
jigga what? were would i put that in the code?
Pages: 12