Question about "IF" and "return"

So my first question is about IF function
int number;
cin>>number;
if (number>99999){
cout<<"Your number has more than 5 digits"<<endl;
return number;}
else if (number<9999){
cout<<"Your number has less than 5 digits"<<endl;
return number;}
else if (number=0){
cout<<"You entered 0... Exiting program"<<endl;
return 0;}
else

My first problem is when i type 0 it doesn't says "You entered 0... Exiting program", but it says "Your number has less than 5 digits". Ive tried to put !0 in else if like "else if (number<9999 && !0)" but that doesn't work.So if anybody could give me a hint it would be nice.
My second problem is that "return number;" doesn't put me back to "int number;" i know i am wrong with this but could someone tell me how it is done. Ty
The equality comparison operator is == in languages with C-derived syntax (C++, Java, C#...).
9999 has less than five digits, by the way.

My second problem is that "return number;" doesn't put me back to "int number;"

Come again?
Edit: are you looking for a loop, perhaps? Read the loop chapter in your book. Keywords: for, while, do while.
Last edited on
I do not understand very well you program, but I write two versions for this. I hope this helps.

Version 1

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
38
39
40
41
// Test - Simple program to test.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int test()
{
    int number;
    cout << "Please, give me a number: ";
    cin >> number;
    
    if (number>99999)
    {
        cout << "Your number has more than 5 digits\n" << endl;
        return number;
    }
    else if (number == 0)
    {
        cout << "You entered 0... Exiting program\n" << endl;
        return number;
    }
    else
    {
        cout << "Your number has less than 5 digits\n" << endl;
        return number;
    }
}

int main(int NumberofArgs, char* pszArgs[])
{
    int num;
    num = test();
    cout << "The number is: " << num << "\n";    
    
    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}



Version 2

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
38
39
40
41
// Test - Simple program to test.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int giveme()
{
    int number = 0;
    cout << "Please, give me a number: ";
    cin >> number;
    return number;
}

int main(int NumberofArgs, char* pszArgs[])
{
    int num;
    num = giveme();
    cout << "The number is: " << num << "\n";
    while (num != 0) 
    {
        if (num > 99999)
        {
            cout << "Your number has more than 5 digits\n" << endl;
            num = giveme();
        }
        else
        {
            cout << "Your number has less than 5 digits\n" << endl;
            num = giveme();
        }
    }
    
    cout << "You entered 0... Exiting program\n" << endl;
    
    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}
sorry if i made it a little understandable it's because i have stumbled upon a problem at that point... so i didn't continue. From here program continues to accept 5 digit numbers until u type 0, and from that point program writes how many 5'ves occur in that 5 digit number per entry... But yeah that helped a lot.
Oh... forgot i had one more question... Why doesn't the return function brings me to "int number; " (to the beginning so i can enter numbers until i press 0) as i wrote "return number;"
"return number" does not return to the location where you declared the variable. It exits the function you are currently in and returns the value.
OH! ty very much now i get it.
I have one more question... is it somehow possible to do this with IF function (i've tried this way but it gives me an error)or maybe there is an better way to do this
(x,y,z are just for example)

IF (x=5 || y=5 || z=5)
cout<<"test";
else
cout<<"test2";

or do i have to write it seperatly

IF (x=5)
cout<<"test";
else (y=5)
cout<<"test";

ect.
Yes but you need to replace = with ==
= is assignemt
== is comparison

1
2
3
4
if(x==5 || y==5 || z==5)
  cout<<"test";
else
  cout<<"test2";
Ty... all.. i completed... the program...
Topic archived. No new replies allowed.