8 Replies - 279 Views - Last Post: 18 October 2012 - 12:57 PM Rate Topic: -----

#1 respect7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Why is my "if" not working ?

Posted 18 October 2012 - 10:53 AM

Hello there, started c++ just last week, running in several problems, here is one of them:

#include <cstdlib>
#include <iostream>

using namespace std;

int a[5];

int calculate()
{
            for(int i=0; i<5; i++){
            cout<<i<<"st number: ";
            cin>>a[i];
            a[i]++;
            }
            cout<<"You wanna know the total?";
            
            if(cin.get() == 'y')
            {
                        int total(); 
            }
            else{
                 cout<<"bye bye";
                 return 0;
                 }
            
}


int total()
{
            int t = 0;
            
            for(int i=0; i<5; i++)
            t+=a[i];
            cout<<"Total is: \n"<<t;
}

    
    
int main(int argc, char *argv[])
{
    
    
            calculate();
            
            
    system("PAUSE");
    return EXIT_SUCCESS;
}



In the calculate function I ask if the person wants to calculate the total of the numbers, it seems to just skip that part.... When I run the program the question appears but it doesn't wait for me to press 'y', it just immediately says "bye bye".

I don't see what I am doing wrong, some help will be appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Why is my "if" not working ?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,680
  • Joined: 25-December 09

Re: Why is my "if" not working ?

Posted 18 October 2012 - 11:07 AM

This is being caused by your extraction operator>> leaving the end line character in the buffer in your for loop. The getchar() will quite happily take that end of line character as it's input. You will need to remove this character before your if statement. Use either cin.ignore() or another cin.get().

Jim
Was This Post Helpful? 1
  • +
  • -

#3 WabiSabi  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 31-December 10

Re: Why is my "if" not working ?

Posted 18 October 2012 - 11:25 AM

In addition to Jim's advice, you need to declare your function total before calling it... and you should call it without the int. But since it returns nothing, I would declare it void total().

And you could just use cin>> instead of cin.get(). This works:

#include <cstdlib>
#include <iostream>

using namespace std;

int a[5];

void total(); //function needs to be declared before using it. you can do this or put the definition here as well as this declaration

int calculate()
{
    char yes;
    for(int i=0; i<5; i++)
    {
        cout<<i<<"st number: ";
        cin>>a[i];
        a[i]++;
    }
    cout<<"You wanna know the total?";


    cin>>yes; //you could use cin>> instead of cin.get() 

    if( yes == 'y')
    {
        //int total(); //call it without the int
        total();
    }
    else
    {
        cout<<"bye bye";
        return 0;
    }

}


//int total()
void total() //an int function returns an int. this one returns nothing so it should be declared void
{
    int t = 0;

    for(int i=0; i<5; i++)
        t+=a[i];
    cout<<"Total is: \n"<<t;

}



int main(int argc, char *argv[])
{


    calculate();


    system("PAUSE");
    return EXIT_SUCCESS;
}

Was This Post Helpful? 1
  • +
  • -

#4 respect7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: Why is my "if" not working ?

Posted 18 October 2012 - 11:32 AM

ok thanks that makes sense but why is it that I have to declare the "Total" funtion and not the "calculate" function? Is that because I call the calculate from "main" and "total" from another function?

I don't get this ^^
Was This Post Helpful? 0
  • +
  • -

#5 respect7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: Why is my "if" not working ?

Posted 18 October 2012 - 11:39 AM

View Postjimblumberg, on 18 October 2012 - 11:07 AM, said:

This is being caused by your extraction operator>> leaving the end line character in the buffer in your for loop. The getchar() will quite happily take that end of line character as it's input. You will need to remove this character before your if statement. Use either cin.ignore() or another cin.get().

Jim


So your saying because of the cin>>a; I have to use a cin.ignore() ? but I don't understand this because I close my for loop with brackets? so why does it see my if(cin.get() == 'y') as input for the [i]for loop?

I feel totally noob asking all those questions, but I try to understand what I am doing... I am sorry for those dumm questions.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,680
  • Joined: 25-December 09

Re: Why is my "if" not working ?

Posted 18 October 2012 - 12:02 PM

Because in your first loop your last entry is actually something like '5' '\n'. I entered 5 for a[4] then I hit the enter key ('\n', end of line character) to complete the entry. The extraction operator places the 5 into the variable but leaves the end of line character in the input buffer. This leftover end of line character is the problem, not your loop, not your actual input. Mixing numeric input with character input is where the problem comes into play. The numeric input always leaves the end of line character in the buffer, numeric input also skips leading white space characters, so until you change to character input you will have no problems. Try this sample program:

#include <iostream>

using namespace std;

int main()
{
   int a;
   cout << "Enter an integer: ";
   cin >> a;

   char skipped;
   cout << "Enter a single character: ";
   skipped = cin.get();

   cout << static_cast<int>(skipped) << endl;

}



Notice that this program skips retrieving the character but still prints something. That something is the end of line character.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 respect7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: Why is my "if" not working ?

Posted 18 October 2012 - 12:13 PM

Alright I start to understand, thanks a lot again!
Was This Post Helpful? 0
  • +
  • -

#8 Rumsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 30-January 11

Re: Why is my "if" not working ?

Posted 18 October 2012 - 12:29 PM

View Postrespect7, on 18 October 2012 - 11:39 AM, said:

View Postjimblumberg, on 18 October 2012 - 11:07 AM, said:

This is being caused by your extraction operator>> leaving the end line character in the buffer in your for loop. The getchar() will quite happily take that end of line character as it's input. You will need to remove this character before your if statement. Use either cin.ignore() or another cin.get().

Jim


So your saying because of the cin>>a; I have to use a cin.ignore() ? but I don't understand this because I close my for loop with brackets? so why does it see my if(cin.get() == 'y') as input for the [i]for loop?

I feel totally noob asking all those questions, but I try to understand what I am doing... I am sorry for those dumm questions.


It's all good buddy, learning should be fun and laid back. In a response to your previous question, you have to declare (prototype) the total function because you call total() in your calculate function before you have defined total.

As for the help already given, cin>>a[i] is fine for reading into the array as cin is an input stream and the >> operator retrieves the formatted data. When you enter numbers cin also picks up the '\n' (new line character) when you hit the Enter key but because it is formatted it does not store it into your array. The very last time you hit Enter, that '\n' is in the cin stream so that cin.get() retrieves unformatted data which means it "will quite happily take that end of line character as it's input". This is why you should use cin>>yes and then check against yes as already stated. If you were really stubborn and for some reason insisted on not reading from the standard input stream you could use cin.ignore(). Useful reading follows:

http://www.cplusplus...e/iostream/cin/
http://www.cplusplus...am/istream/get/
http://www.cplusplus...istream/ignore/

Best regards,
Rumsy
Was This Post Helpful? 1
  • +
  • -

#9 WabiSabi  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 31-December 10

Re: Why is my "if" not working ?

Posted 18 October 2012 - 12:57 PM

View Postrespect7, on 18 October 2012 - 12:32 PM, said:

ok thanks that makes sense but why is it that I have to declare the "Total" funtion and not the "calculate" function? Is that because I call the calculate from "main" and "total" from another function?

I don't get this ^^


Because your calculate() function is defined above where you call it. The compiler reads down line by line, and if you don't at least let it know what's there (by either declaring or defining the function above where it's called) then it won't know about that function when it gets to the line where you call the function, and it will cause an error.

If you don't want to have to worry about where to define all your functions, or you want to define them all after main or something like that... then you might want to get in the habit of just declaring every function right at the top of your program.

More about declare/define:
http://www.cprogramm..._vs_define.html

This post has been edited by WabiSabi: 18 October 2012 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1