freeze with apples and cout

can som one plz help. my program crashes when it shows how many are left.

#include <iostream>
#include <limits>
using namespace std;

int main()
{
int apples;
cout << "how many apples :";
cin >> apples;
int take;
cout << "take apples";
cin >> take;
int Left = apples-take;
if(Left < 0)
{
cout << "not enough aples" << endl;
}
else if(take = apples)
{
cout << "no apples left" << endl;
}
else
{
cout << "nunber of apples: " << left;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

sorry i dont know how to keep the spaces in front
Last edited on
What kind of apples are you using?
I see the problem. "int apples" is apples, but "int take" is oranges, and you can't compare apples and oranges so the program fails on else if(take = apples).
Wrong, it is failing because he didn't use code tags! :P
cin.ignore(numeric_limits<streamsize>::max(), '\n');

You should use std::system("PAUSE");.
i cant my prof says i have to use dev cpp
i cant my prof says i have to use dev cpp

We deprecated that version of C++.
closed account (N36fSL3A)
I'm taking this as a joke.

I'm seriously dying laughing right now.
Last edited on by Fredbill30
Yes, this was a satirization of those threads that just make you feel like society is turning into mush. The original point of the code was to demonstrate why "using namespace std;" is bad and why your compiler won't help you fix problems with using the wrong name, but I decided to go for the satire.
Last edited on
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 <iostream>

int main()
{
	/*
	      ,---. 
            ,.'-.   \ 
           ( ( ,'"""""-. 
           `,X          `. 
           /` `           `._ 
          (            ,   ,_\ 
          |          ,---.,'o `. 
          |         / o   \     ) 
           \ ,.    (      .____, 
            \| \    \____,'     \ 
          '`'\  \        _,____,' 
          \  ,--      ,-'     \ 
            ( C     ,'         \ 
             `--'  .'           | 
               |   |         .O | 
             __|    \        ,-'_ 
            / `L     `._  _,'  ' `. 
           /    `--.._  `',.   _\  ` 
           `-.       /\  | `. ( ,\  \ 
          _/  `-._  /  \ |--'  (     \ 
         '  `-.   `'    \/\`.   `.    ) 
               \      \ `.  |    | 
	
	*/
	return 0;
}
Last edited on
What's with the random iostream include? Does your ASCII art really need to use the bathroom that much?
Does your ASCII art really need to use the bathroom that much?

That made me laugh a little too much... I had a couple co-workers look at me oddly.

I did have std::cout originally in there and std::cin.ignore as well, but I took those out to reduce clutter and I guess I overlooked the #include .
I was enjoying this up until post 9. Now I'm confused. (I see no namespace error in the code. Am I just missing something really obvious?)
I was enjoying this up until post 9. Now I'm confused. (I see no namespace error in the code. Am I just missing something really obvious?)


Hmm, std::left?
I tryed this:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main()
{
    int left = 5;
    cout << left << endl;
    return 0;
}

And it compile and printed 5 as expected. std::left is part of <ios>, so maybe you would need to include that to have failure?

Edit:
I was wrong, it wont fail. Heh, possibly that you declare 'left' as a variable, you can use it as a variable. I'm too lazy to try creating a conflict between std::left and the variable int left right now though.
Last edited on
The compiler always uses the one in the innermost scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int left = 0;

int main()
{
    int left = 2;
    {
        int left = 4;
        {
            int left = 6;
            std::cout << left;
        }
        std::cout << left;
    }
    std::cout << left << ::left;
    return 0;
}
6420

The only way they could conflict is if they were in the same scope, so if I put using namespace std; my global int left would conflict with std::left.
@Oria have you forgotten the fact that C++ is case-sensitive? Check my first post again to see how the variable is initially declared.
Yes, compiler will use inner scope bindings when you rebind an identifier name. Compiler that fails == bad compiler.

But also, OP uses "Left", which is not in std::

@Oria
All the standard streams headers automatically include <ios>.

Topic archived. No new replies allowed.