#need help with printing real numbers

50 messages · Page 1 of 1 (latest)

teal dove
#

Here’s the code, the only issue seems to be using cout on variables, specifically double ones, that have numbers in them, im not sure how it works, here’s the code:

#include <iostream>
using namespace std;

int main()
{
int drink;
int log1;// insert price for drink one; coffee
int log2;// insert price for drink two; lemonade
int log3;// insert price for drink three; coca cola
int log4;// insert price for drink four; boba tea
double price1 = 2.99;// set price for drink 1; coffee
double price2 = 1.50;// set price for drink 2; lemonade
double price3 = 3.50;// set price for drink 3; coca cola
double price4 = 4.99;// set price for drink 4; boba tea

cout << "1.coffee" <<endl;
cout << "2.lemonade" <<endl;
cout << "3.coca cola" <<endl;
cout << "4.boba tea" << endl;
cout << "select a drink number: ";
cin >> drink; // where the drink is selected; from the 4 options avalable

if (drink == 1)// price testing for drink 1; coffee
{
cout << " price:"
cout << price1;
cout << endl;
cout << " insert money: "
cin >> log1;
}
else if (drink == 2)// price testing for drink 2; lemonade
{
cout << " price:"
cout << price2;
cout << endl;
cout << " insert money: "
cin >> log2;
}
else if (drink == 3)// price testing for drink 3; coca cola
{
cout << " price:"
cout << price3;
cout << endl;
cout << " insert money: "
cin >> log3;
}
else if ( drink == 4)//price testing for drink 4; boba tea
{
cout << " price:"
cout << price4;
cout << endl;
cout << " insert money: "
cin >> log4;
}
switch (drink)//drink outputs
{
case 1:
cout << "coffee dispensed";
break;
case 2:
cout << "lemonade dispensed";
break;
case 3:
cout << "coca cola dispenced";
break;
case 4:
cout << "boba tea dispensed";
break;
}
}

burnt scaffoldBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

burnt scaffoldBOT
#

Here’s the code, the only issue seems to be using cout on variables, specifically double ones, that have numbers in them, im not sure how it works, here’s the code:

#include <iostream>
using namespace std;

int main() {
  int drink;
  int log1;              // insert price for drink one; coffee
  int log2;              // insert price for drink two; lemonade
  int log3;              // insert price for drink three; coca cola
  int log4;              // insert price for drink four; boba tea
  double price1 = 2.99;  // set price for drink 1; coffee
  double price2 = 1.50;  // set price for drink 2; lemonade
  double price3 = 3.50;  // set price for drink 3; coca cola
  double price4 = 4.99;  // set price for drink 4; boba tea

  cout << "1.coffee" << endl;
  cout << "2.lemonade" << endl;
  cout << "3.coca cola" << endl;
  cout << "4.boba tea" << endl;
  cout << "select a drink number: ";
  cin >> drink;  // where the drink is selected; from the 4 options avalable

  if (drink == 1)  // price testing for drink 1; coffee
  {
    cout << " price:" cout << price1;
    cout << endl;
    cout << " insert money: " cin >> log1;
  } else if (drink == 2)  // price testing for drink 2; lemonade
  {
    cout << " price:" cout << price2;
    cout << endl;
    cout << " insert money: " cin >> log2;
  } else if (drink == 3)  // price testing for drink 3; coca cola
  {
    cout << " price:" cout << price3;
    cout << endl;
    cout << " insert money: " cin >> log3;
  } else if (drink == 4)  // price testing for drink 4; boba tea
  {
    cout << " price:" cout << price4;
    cout << endl;
    cout << " insert money: " cin >> log4;
  }
  switch (drink)  // drink outputs
  {
    case 1:
      cout << "coffee dispensed";
      break;
    case 2:
      cout << "lemonade dispensed";
      break;
    case 3:
      cout << "coca cola dispenced";
      break;
    case 4:
      cout << "boba tea dispensed";
      break;
  }
}
Faith_TheOneTheOnly
neon grove
#

cout << " price:" cout << price1;
How is this even compiling

bold ospreyBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:24:22: error: expected ';' before 'cout'
   24 |     cout << " price:" cout << price1;
      |                      ^~~~~
      |                      ;
<source>:26:30: error: expected ';' before 'cin'
   26 |     cout << " insert money: " cin >> log1;
      |                              ^~~~
      |                              ;
<source>:29:22: error: expected ';' before 'cout'
   29 |     cout << " price:" cout << price2;
      |                      ^~~~~
      |                      ;
<source>:31:30: error: expected ';' before 'cin'
   31 |     cout << " insert money: " cin >> log2;
      |                              ^~~~
      |                              ;
<source>:34:22: error: expected ';' before 'cout'
   34 |     cout << " price:" cout << price3;
      |                      ^~~~~
      |                      ;
<source>:36:30: error: expected ';' before 'cin'
   36 |     cout << " insert money: " cin >> log3;
      |      
neon grove
#

@teal dove first of all

teal dove
#

All I need to know is how to print numbers in the console, in the form of variables

#

Basically how do I print a double variable

neon grove
#

Your code doesn't even compile

#

as made clear above

teal dove
#

Dude I'm a beginner, I gotta make things more simple

#

For me I mean

#

What does it mean

neon grove
#

Your code won't run

teal dove
#

No shit

neon grove
# bold osprey

If you click build on your editor you'll get errors similar to this

teal dove
#

I know, so how do I solve them

neon grove
#

semicolons

#

and I'd suggest you put each statement in a line

#

it makes your code a lot more readable

teal dove
#

Well this is how I read the code, so anyway, as I already asked, how to I use cout on a double

median marlin
#

error: expected ';' before 'cout'

error: expected ';' before 'cin'
The errors should be pretty self-explanatory

teal dove
#

Wait I fixed it

#

Ok

#

Hold on

neon grove
teal dove
#

Ok thanks

neon grove
#

You could also

teal dove
#

I might be posting again soon for some other part of this code but it works right now

median marlin
#

Also the code itself is questionable.

Why do you have a switch after an if/else that does exactly the same thing as that switch does?

#

Also you have a lot of duplicate code

neon grove
#
- cout << " price:" cout << price2;
- cout << endl;
- cout << " insert money: " cin >> log2;
+ cout << "price: " << price 2 << endl << " insert money: ";
+ cin >> log2;
#

But the most sane option if you're using C++20 is std::format

#

Also what Monke said, you have duplicate code

median marlin
#

Oh, and also:

#

!wiki Why Is using namespace std Considered Bad Practice?

burnt scaffoldBOT
# median marlin !wiki Why Is `using namespace std` Considered Bad Practice?
Why Is `using namespace std` Considered Bad Practice?

using namespace std will import all the symbols from std into the enclosing namespace. This can easily lead to name collisions, as the standard library is filled with common names: get, count, map, array, etc.

A key concern with using namespace std; is not what is imported now but rather what may suddenly be imported in the future.

While using namespace std; is alright for tiny projects, it is important to move away from it as soon as possible. Consider less intrusive alternatives:

// OK: *only* import std::vector
using std::vector;
// OK: namespace alias
namespace chr = std::chrono;
chr::duration x;
teal dove
neon grove
#

Since he's a beginner I think he'll be fine to use std

neon grove
#

you don't need to

median marlin
neon grove
#

Yeah, general rule in programming is avoiding doing things more than once

median marlin
#

Also you never use your log variables

teal dove
#

!solved

burnt scaffoldBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

neon grove
#

Oh, well

median marlin
# neon grove Oh, well

Yeah, I was just about to write them a better implementation, but apparently they aren't interested and I don't think they even wanted to hear how they could improve their code, they just wanted to get it working