Question:
Still more help needed in C++ programming problem using Dev C++?
2009-10-16 15:48:53 UTC
I had some great suggestions from someone earlier, but that didn't completely solve my issue either. Here is what I now have, and it is not working. I have a tight deadline that I have to meet tonight so I would appreciate any and all help.

#include

using namespace std ;

int main()
{

// Declare fruit, weight and cost as variables.
char fruit;
float cost;

cout << endl;
cout << "Enter type of fruit(apples, pears, pecans, or oranges): ";
cin >> fruit;

// Write C++ if-then-else statements to determine fruit cost,

if (char = "apples");
cost = 1.59;
else if (char = "oranges");
cost = 1.29 ;
else if (char = "pecans");
cost = 2.59 ;
else if (char = "pears");
cost = .99 ;

// display the result

cout << endl ;
cout << "Your cost is: " << cost << endl ;

system("pause");

return (0); // terminate with success
}
Five answers:
Razza
2009-10-16 16:01:35 UTC
Hey. There's a couple of problems there.

Firstly, char fruit, is only allocating memory for a singe character, so you'll need something like, char fruit[100].

Secondly, if (char = "apples");, is not only comparing the wrong thing (should be fruit), you can't compare strings like that, and even if you could, it would need to be == instead of =, as = will assign a new variable to it and not compare. You can fix this by using strcmp(). And finally, the ; after the if statements will 'cause them to end imediately. You might need to #include as well.

This should fix it all.



#include



using namespace std ;



int main()

{



// Declare fruit, weight and cost as variables.

char fruit[100];

float cost;



cout << endl;

cout << "Enter type of fruit(apples, pears, pecans, or oranges): ";

cin >> fruit;



// Write C++ if-then-else statements to determine fruit cost,



if (strcmp(fruit,"apples")==0)

cost = 1.59;

else if (strcmp(fruit,"oranges")==0)

cost = 1.29;

else if (strcmp(fruit,"pecans")==0)

cost = 2.59;

else if (strcmp(fruit,"pears")==0)

cost = .99;



// display the result



cout << endl ;

cout << "Your cost is: " << cost << endl ;



system("pause");



return (0); // terminate with success

}



Hope that helps :)
2009-10-16 15:57:41 UTC
include



using namespace std ;



int main()

{



// Declare fruit, weight and cost as variables.

string fruit; //string, char can only take 1 letter

float cost;



cout << endl;

cout << "Enter type of fruit(apples, pears, pecans, or oranges): ";

cin >> fruit;



// Write C++ if-then-else statements to determine fruit cost,



if (fruit == "apples") //changed char to fruit, the variable name

cost = 1.59;

else if (fruit == "oranges") //no semicolons for if else statements

cost = 1.29 ;

else if (fruit == "pecans") //also its "==" not just "="

cost = 2.59 ;

else if (fruit == "pears")

cost = .99 ;



// display the result



cout << endl ;

cout << "Your cost is: " << cost << endl ;



system("pause");



return (0); // terminate with success

}
walmeis
2009-10-16 16:01:21 UTC
There are multiple issues:

* "char fruit" only holds a single character. Perhaps you want a string for the type?

* = (a single equal sign) is an assignment operator. Always. For the comparisons, use the compare for equality operator, == (double equal sign).

* the semicolons after if (expression) cause the if statement to do nothing; additionally, the "cost =" statements are not in a recognized position since the "then" part of the if is "used up" by the null statement between ) and ;

* ".99" might not be valid. "0.99" definitely is valid.
2009-10-16 15:58:07 UTC
You need to specify the length of the char type. You need to put something like char fruit[7]; This tells the computer to allocate 7 chunks of size char for a variable. You can't just say char fruit;. Also, when doing tests for equality, you need to use == not =. = means set a variable to something. == means check to see if equal.
reuter
2016-10-22 05:20:49 UTC
you are able to in basic terms use this attempt if(a[i]==b[j]) for a single length array. it extremely is that if the variable a[i] and b[j] come to a determination to single components. however in easily reality the attempt could relatively be if(a[i]==b[i]) have relaxing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...