5 Replies - 219 Views - Last Post: 25 January 2013 - 06:37 PM Rate Topic: -----

#1 Nougati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-January 13

Misunderstanding with how to use an integer w/ a very simple program

Posted 25 January 2013 - 06:59 AM

Hey hey,

this is my first time ever on the site posting, and as such, if you are reading this, I need you to understand that I am really, really new to C++. I've only even done Flash Actionscript, HTML and Javascript in the past but nothing in great detail as they stopped as my school curriculum stopped teaching it.
Hence I'm using this video series by DevHQLessons on youtube to help me out with learning the basics.
I just watched video on Class Members, and I think I get the concept, and in an attempt to put my knowledge to a bit of fun I tried to expand on what he did on the video (If you're interested).
Anywho this coding for a Win32 Console Application, and the idea of my program at this state is to ask the user what they would like to order from a takeaway restaurant, by answering y/n to the prompts and then, if needed, specifying the quantity of each product, then listing them down at the end. My first idea for specifying the quantity was to use an integer, however this posed me some problems.

The output at the end of the questions only ever lists how much chicken is ordered, nothing else. Additionally, the compiler freaks out if I don't initialize variables "chickens", or if I answer no to all the prompts. I hope I make sense, and if I don't, I'm incredibly sorry.

Here is the code, if relevant:
#include <iostream>

using namespace std;

class takeAway{
public:
void wantsChicken(){
	chicken = true;
}
void wantsNoodles(){
	noodles = true;
}
void wantsBurger(){
	burger = true;
}
bool chicken;
bool noodles;
bool burger;
};



int main(){
	char yn1;
	int qty1;
	char yn2;
	int qty2;
	char yn3;
	int qty3;
takeAway Customer1;
	cout << "You want chicken? Y/N" << endl;
	cin >> yn1;
	if(yn1 == 'y' || yn1 == 'Y') {
		Customer1.wantsChicken();
	cout << "How many chickens?" << endl;
	cin >> qty1;
	}

	cout << "You want noodles? Y/N" << endl;
	cin >> yn1;
	if(yn1 == 'y' || yn1 == 'Y') {
		Customer1.wantsChicken();
	cout << "How many noodles?" << endl;
	cin >> qty2;
	}

	cout << "You want burgers? Y/N" << endl;
	cin >> yn1;
	if(yn1 == 'y' || yn1 == 'Y') {
		Customer1.wantsChicken();
	cout << "How many burgers?" << endl;
	cin >> qty3;
	}
	cout << "You have ordered:" << endl;
	if(Customer1.chicken == true){
		cout << "- " << qty1 << "x Chicken(s)" << endl;
	}
	if(Customer1.noodles == true){
		cout << "- " << qty1 << "x Noodle(s)" << endl;
	}
	if(Customer1.burger == true){
		cout << "- " << qty1 << "x Burger(s)" << endl;
	}
	system("PAUSE");
	return 0;
}


Hopefully this is all covered, and if you could help, that would be much appreciated. I'm really hoping I can get some skills together with this language to the point of one day learning how to make a basic game.

Is This A Good Question/Topic? 1
  • +

Replies To: Misunderstanding with how to use an integer w/ a very simple program

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: Misunderstanding with how to use an integer w/ a very simple program

Posted 25 January 2013 - 07:09 AM

    cout << "You want noodles? Y/N" << endl;
    cin >> yn1;
    if(yn1 == 'y' || yn1 == 'Y') {
        Customer1.wantsChicken();


You are asking if they want noodles but calling the function to confirm that they want chicken..

Your restaurant only serves chicken :)/>

This post has been edited by andrewsw: 25 January 2013 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 Nougati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-January 13

Re: Misunderstanding with how to use an integer w/ a very simple program

Posted 25 January 2013 - 07:56 AM

View Postandrewsw, on 25 January 2013 - 07:09 AM, said:

    cout << "You want noodles? Y/N" << endl;
    cin >> yn1;
    if(yn1 == 'y' || yn1 == 'Y') {
        Customer1.wantsChicken();


You are asking if they want noodles but calling the function to confirm that they want chicken..

Your restaurant only serves chicken :)/>/>



Okay so I think I've overcome the fact that I'm wildly embarrassed so I'm ready to reply. For the record, that can serve as a lesson to take more care with my pasting hahahah.

Okay hate to be a pest, but one last thing: It lists everything nicely and perfectly as intended (yay!)!
Except if chicken is not specified at all (if I select N for chicken) then the compiler goes

Quote

"Run-Time Check Failure #3 - The variable 'qty1' is being used without being initialized."

I hate to be annoying but I seriously haven't a clue on how to remedy this last issue.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: Misunderstanding with how to use an integer w/ a very simple program

Posted 25 January 2013 - 08:09 AM

cout << "- " << qty1 << "x Noodle(s)" << endl;


qty1 is in response to the question about chicken.. which, if they don't want chicken, generates the error message - it hasn't been assigned a value.

qty2 is the value that relates to noodles.. and qty3..
Was This Post Helpful? 0
  • +
  • -

#5 Nougati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-January 13

Re: Misunderstanding with how to use an integer w/ a very simple program

Posted 25 January 2013 - 05:29 PM

View Postandrewsw, on 25 January 2013 - 08:09 AM, said:

cout << "- " << qty1 << "x Noodle(s)" << endl;


qty1 is in response to the question about chicken.. which, if they don't want chicken, generates the error message - it hasn't been assigned a value.

qty2 is the value that relates to noodles.. and qty3..


Can you tell I'm bad at this? Haha

Thanks for your help, I think I get where I went wrong now haha. :D
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Misunderstanding with how to use an integer w/ a very simple program

Posted 25 January 2013 - 06:37 PM

Quote

I'm really hoping I can get some skills together with this language to the point of one day learning how to make a basic game.


Quote

Can you tell I'm bad at this? Haha


Sorry if this is a bit off topic, and I admit to not fully reading the question properly, but I want to say that you seem very under-confident. There's nothing to be under-confident about, just have confidence in what you know, and have the ambition to understand all that you don't yet know, and of course, be able to distinctly say what you do and don't understand.

I think that this is some advice which may be useful to you, as a person who went through a lot of self doubt early on.

This post has been edited by Aphex19: 25 January 2013 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1