Hi all, I am very new so excuse the messy code. I am just trying to get this code to work properly first before cleaning it :p . For now I want it to: if user types "no" exit, if user types "yes" then run the next part, or if the user inputs anything else, it repeats the previous step (asking yes or no). At the moment, if the user types anything but "yes" it still tries to give it a price for some reason. I also want it to say the quantity of the apples, but unsure how to make a function return more than 1 thing.

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

int quant(int qnty, int price)
{
	int sum;
	sum = qnty * price;
	return (sum);
}

int apples(int a)
{
string yn;
int answer, qnt;
int appleprice = 1.2;

cout << "\nWould you like some apples?";
	cin >> yn;
	if (yn == "yes" || "y" || "yeah" || "yep")
	answer = 1;

if (yn == "no")
    answer = 2;

	switch(answer)
	{
		case 1:
		{
		cout << "\nHow many apples would you like? They are $1.20 ea.";
		cin >> qnt;
		quant(qnt,appleprice);
	}
	break;
	case 2:
	{cout << "\nOkay";
}
break;
default:
{cout<<"\nlolwut?";
return 0;
}
}
cout << "\nThe price for " << qnt << " is: " << appleprice;
return (a);
}
	
int main()
{
	int a;
	cout << "\nWelcome to the fruit shop!";
	cout << "\nWe only have apples at the moment.";
	apples(a);
	cout << a; //It keeps telling me "a" is uninitialized, which I dont care, is
                                                  //there a better way to word this?
	main();
	return 0;
}

Edited 5 Years Ago by Gobbler: n/a

if (yn == "yes" || "y" || "yeah" || "yep")

doesn't work, it must be

if(yn=="yes" ||yn=="y"||yn=="yeah"||yn=="yep")

On line 32, you're not using the return value of quant anyway, but to return more than one thing from a function, use references:

//sending in 2 references
void myfunc(int & a,int & b) //or you can return one the "old fashioned way" and send the other in by reference
{
   a++;  //modifying the actual value of what we're passin in as "a" in here.
   b+=3;
}

int main()
{ 
   int x = 4;
   int y = 5;
   myfunc(x,y);
   cout<<x<<endl; //5
   cout<<y<<endl; //8
}

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.