• 0

C++ Project Help


Question

Hi, I am new to C++ and I have a project due on friday. This is out first week with structures and I am struggling. I am trying to be able to pick a plant, get a count on how many of this plant and then calculate a price....each thing having a seperate function. Can someone help me out with what I am doing wrong on the third function?

#include

using namespace std;

struct plant

{

int choice;

float price;

float quantity;

};

plant pick(plant tulip);

//Pre: none

//Post: user has been informed of choices and passed on valid choice

//plant cost(plant tulip);

//Pre: need choice to be made and valid, and number of plants

//Post: cost of plant wanted * number desired

plant amount(plant tulip);

plant cost( int choice, int quantity, plant tulip);

int main()

{

plant plantChoice, plantQuantity, plantCost;

int totalCounter, counter;

plantChoice.choice = 1;

plantChoice.price = 1;

plantChoice.quantity = 1;

//plantChoice = pick(plantChoice);

//plantQuantity = amount(plantQuantity);

plantCost = cost( plantChoice, plantQuantity, plantCost);

return 0;

}

plant pick(plant tulip)

{

int counter;

cout << "Follow the onscreen instructions and press 0 to quitn"; //directions

cout << "The following plants are available: nn";

cout << "Plant#tPricen1t4.95n2t2.95n3t14.95n4t12.95n5t7.95n" << endl; //plant info

cout << "Please enter the plant# you would like (0 to quit): ";

cin >> tulip.choice;

while ((tulip.choice >5) || (tulip.choice <0)) // makes sure the plant# entered is valid

{

cout << "Enter a valid plant#.n";

cin >> tulip.choice;

}

switch (tulip.choice) //sets the price of the plant

{

case 0:

break;

case 1:

tulip.price=(float)4.95;

break;

case 2:

tulip.price=(float)2.95;

break;

case 3:

tulip.price=(float)14.95;

break;

case 4:

tulip.price=(float)12.95;

break;

case 5:

tulip.price=(float)7.95;

break;

default:

cout << "You did not enter a correct plant#." << endl;

}

return tulip;

}

plant amount(plant tulip)

{

cout << "How many sold: ";

cin >> tulip.quantity;

if (tulip.quantity < 0) //makes sure the number entered is positive

{ cout << "nPlease enter a positive number this time..." << endl;

cout << "How many sold: ";

cin >> tulip.quantity;

}

return tulip;

}

plant cost( int choice, int quantity, plant tulip)

{

choice = pick(tulip)

quantity = amount(tulip)

float price;

switch (choice) //sets the price of the plant

{

case 0:

break;

case 1:

price=(float)4.95;

break;

case 2:

price=(float)2.95;

break;

case 3:

price=(float)14.95;

break;

case 4:

price=(float)12.95;

break;

case 5:

price=(float)7.95;

break;

default:

cout << "Something went wrong!" << endl;

}

return tulip;

}

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

The problem is that you're trying to redefine parameters that are passed to your function. You can't do this.

Originally posted by aitf311
&lt;pre&gt;

plant cost( int choice, int quantity, plant tulip)

{

	choice = pick(tulip)

	quantity = amount(tulip)



        ******snip******



        return tulip;

}

&lt;/pre&gt;

Here is what you should have done instead:

&lt;pre&gt;

plant cost(plant tulip)

{

        int choice = pick(tulip);

        int quantity = amount(tulip);



        ******snip******



        return tulip;

}

&lt;/pre&gt;

Variables that you need to use inside the function should be declared inside the function if you need to modify them.

Link to comment
Share on other sites

  • 0

Yeah, I just had an assignment to do last night with structs. Stayed up late doing it. I had to make members of structs within another struct and one of the structs had to keep all the data of the other stucts in an array. Now I have to do the same program only with classes bleh :dead:

If you need some help with some assignments, send me a PM on the board and I'll offer some advice. At least we can both learn by explaing stuff to each other.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.