Array Problem

Why won't it print the input value I give it for buy[0] after I press '1'?

cout << "Quantity: ";
cin >> buy[0];
cout << buy[0] << " gallons of water purchased!";
b = getch();
if (b=='1')
cout << buy[0];

Thanks,
Timmah m/
closed account (S6k9GNh0)
If I'm not mistaken, getch() isn't defined by either C or C++ standard. Using a test case and replacing getch() with std::cin.get(), I get http://ideone.com/clone/Agb0h

I can't do anything else for ya without more info.
closed account (zb0S216C)
If I'm not mistaken, getch() isn't defined by either C or C++ standard

So what. If it isn't defined by the standard it doesn't mean that you cannot use it( if this is what you mean ). I use getch( ) all the time. All you need is the conio.h header to access it.
Are you storing ints or chars?

'1' does not equal 1. Check out the ASCII table, if I remember right '0' starts at 48, so try this with the number 49...
closed account (S6k9GNh0)
So, you'd rather lose cross-platform capabilities just to use getch() instead of getchar() or std::cin.get()? Might I add that most online sample compilers provided by sites such as codepad.org or ideone.com don't allow getch().

Aside from that, I'm not saying that's his problem. We can't determine his problem without more information.
Last edited on
closed account (zb0S216C)
you'd rather lose cross-platform capabilities just to use getch() instead of getchar() or std::cin.get()?

As I've stated numerous times, I only program under the Windows OS. I wrote my previous post based on what I interpreted from your previous post.
closed account (S6k9GNh0)
That doesn't mean you should discourage the use of efficient and flexible methods.
Framework, we've deprecated the conio.h header simply because it's old and tends to vary in functionality between implementations. I'd like to suggest that you use something newer and less random for your projects. :|

-Albatross
closed account (zb0S216C)
we've deprecated the conio.h header simply because it's old

I'm still learning C so I've kind of grown used to using old methods.
Well... it's your programing style, I guess. conio.h is still being (somewhat) maintained by Microsoft, so I suppose it's not a case of severe deprecation.

Still, using a header whose functions vary somewhat from compiler to compiler doesn't strike me as a good idea. :/

-Albatross
Last edited on
it has nothing to do with the getche()

What I'm doing is inputing a value for a space in my array (buy[0]), and then testing to see whether or not it's stored. The b = getch() thing is just my way of testing if it's still stored. When I press 1 after it's stored, it should print buy[0], or the value I inputed. Yes? No? D:

And I would be storing an int.
Last edited on
closed account (zb0S216C)
Of what type is b?
The b = getch() thing is just my way of testing if it's still stored

If this is the case then getch( ) will overwrite the current value stored in b; Unless this is what you're after.
There is no value stored in b. The value is stored in buy[0], and then I just need some variable for my keyboard input, as in when b = keyboard input of 1, then print buy[0] (which is what i inputed before.
Here, let me write out what I'm trying to do in words to make it easier for you all to understand:

All I am trying to do is better understand Arrays and make sure I know what I'm doing before I move on. So, first, the program takes an input value, and stores that value in space 0 of buy[]. (cin >> buy[0]) The value stored is an interger (2 for example). Next, it prints a line of text. Then it waits for another input from the keyboard (b = getch() this is the test part). I'm using b just as a random variable for this input value. If this input turns out to be 1, then it will print buy[0], or 2 in this case. Why is this not working?

Also, side note. I tried changing if (b=='1') to if (b=='49') and it worked.
Last edited on
Hi ,
The neat way of doing this is
1
2
3
4
5
6
7
8
9
         char buy[10]; 
	cout << "Quantity: ";
	cin >> buy;
	cout << buy << " gallons of water purchased!";
	char b ;
	cout<<"n Enter 1";
	cin >>b ;
	if (b=='1')
		cout << buy; 
why does this work? Which space (0-10) is the input value for buy being asigned to?

why does buy[10] and b both have to be char?
Topic archived. No new replies allowed.