#Help with a simple beginner program. Not understanding where I went wrong...

54 messages · Page 1 of 1 (latest)

magic haloBOT
#

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.

#
How to Ask a Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What to Post

State your problem clearly and provide all necessary details:

  • the relevant portion of your code, or all of it
  • the expected output
  • the actual output (or the full error)
    :trophy: Gold Standard: Minimal Reproducible Example
Where to Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:

  • Compiler Explorer for most C and C++ snippets
  • OnlineGDB for interaction, debugging
    :no_entry: Do not post screenshots, let alone photos of your screen!
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
green latch
#

copy paste the code and the errors

#

code first

dawn bolt
#

I keep getting this when trying to format the code :/

#

Your message could not be delivered. This is usually because you don't share a server with the recipient or the recipient is only accepting direct messages from friends. You can see the full list of reasons here:

green latch
#

restart discord

#

that might fix it

#

ctrl + R

dawn bolt
#

Nah it's still doing it. I went into my setting to change things and everything 😦

green latch
#

how long is your code?

dawn bolt
#

With comments included 77 lines

green latch
#

Not sure why it's not letting you post it then. Just try to post it without formatting it

dawn bolt
#

//A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm
//ships cartons of milk to a local grocery store. The cost of producing
//one liter of milk is $0.38, and the profit of each carton of milk is
//$0.27. Write a program that does the following:
//a. Prompts the user to enter the total amount of milk produced in the
//morning.
//b. Outputs the number of milk cartons needed to hold milk. (Round
//your answer to the nearest integer.)
//c. Outputs the cost of producing milk.
//d. Outputs the profit for producing milk.

//Include header file to allow input/output

#include <iostream>

//Include directive to avoid unnecessary repetition.

using namespace std;

//Declare a constant to represent the capacity of a carton of milk.

const double carton = 3.78;

//Create main function.

int main();

{

//Declare variables to represent milk, milk cartons, cost
//to produce, profits, cost per milk produced, and profit
//per milk produced.

double milk;
int milk_Carton;
double cost = 0.38;
double profit = 0.27;
double cost_Per, profit_Per;

//Prompts the user to enter the total amount of 
//milk produced in the morning.

cout << "Enter, in liters, the total quantity of milk produced: " << endl;

//Assign the user's input to the milk variable.

cin >> milk;

//Assign value to previously declared variable milk_carton.

milk_Carton = milk / carton;

//Outputs the number of milk cartons needed to 
//hold milk. (Round your answer to the 
//nearest integer.)

cout << "The number of milk cartons needed to hold milk: " << milk_Carton << endl;

//Assign values to previously declared variables cost_Per
//and profit_Per.

cost_Per = milk * cost;
profit_Per = milk_Carton * profit;
#

I think it's because I don

#

dont have nitro. This isn't the full code by the way

#

Let me delete the comments real quick and see if itll allow me to then

#
//A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm 
//ships cartons of milk to a local grocery store. The cost of producing
//one liter of milk is $0.38, and the profit of each carton of milk is
//$0.27. Write a program that does the following:
//a. Prompts the user to enter the total amount of milk produced in the
//morning.
//b. Outputs the number of milk cartons needed to hold milk. (Round
//your answer to the nearest integer.)
//c. Outputs the cost of producing milk.
//d. Outputs the profit for producing milk.

//Include header file to allow input/output

#include <iostream>

//Include directive to avoid unnecessary repetition.

using namespace std;

//Declare a constant to represent the capacity of a carton of milk.

const double carton = 3.78;

//Create main function.

int main();

{

    double milk;
    int milk_Carton;
    double cost = 0.38;
    double profit = 0.27;
    double cost_Per, profit_Per;

    cout << "Enter, in liters, the total quantity of milk produced: " << endl;

    cin >> milk;

    milk_Carton = milk / carton;

    cout << "The number of milk cartons needed to hold milk: " << milk_Carton << endl;

    cost_Per = milk * cost;
    profit_Per = milk_Carton * profit;

    cout << "The cost of producing milk: " << cost_Per << endl;

    cout << "Profit: " << profit_Per << endl;

    return 0;

}
#

There we go!

green latch
#

What are the errors?

dawn bolt
#

it's saying my identifiers are undefined

#

and an expected declaration in line 28

#

^this is the first error

green latch
#

You can either copy paste the errors or screenshot them

#

It would be tedious to describe all 18 errors lol

dawn bolt
#

Sorry about that

green latch
#

Can you see what's wrong with this part of the code?

int main();

{
dawn bolt
#

is the semi colon unnecessary?

green latch
#

yes

dawn bolt
#

Wow I feel dumb that cleared everything but one issue

green latch
#

It's like you're doing forward declaration if you use semicolon after a function

dawn bolt
#

Gotcha. So no function uses the semi colon at the end?

green latch
#

If you're forward declaring a function because the function is below the main function you would use it and some other specific cases but other than that, no.

dawn bolt
#

Oh it ran!

#

Oh so you forward functions that are within the main function?

#

This is like my 3rd week into c++

#

Super intimidating so far

green latch
#

for example

int add(int a, int b);

int main(){
int a = 5; 
int b = 6;
std::cout << add (a, b);
}

int add(int a, int b){
return a + b;
}
dawn bolt
#

I haven't learned any functions other than main yet

#

But that's really interesting

green latch
dawn bolt
#

It took me forever 😅

green latch
#

It's already being practical

#

Using the right type of resources as a beginner is pretty helpful, I'll leave you with some below that are recommended

magic haloBOT
#
How to Learn C++ Programming

We generally recommend a good book to learn the necessary fundamentals:

To actually write and run C++ code, you will need a compiler, editor, and debugger. We strongly recommend to start out using an IDE, which will provide all these tools for you:

<:microsoft:1165512917047853127> Windows
  • [Visual Studio](#1165492293810257920 message)
  • CLion
<:tux:1165505626894520361> Linux
<:apple:1165508607798943754> Mac
Words of Advice

The wise programmer is told about the debugger and uses it.
The average programmer is told about the debugger and avoids it.
The foolish programmer is told about the debugger and laughs at it.

dawn bolt
#

I'm learning from the D.S. Malik C++ Programming textbook right now. This thing is like a phonebook lol

green latch
#

learncpp is free from the three of them and a lot of people recommend it

dawn bolt
#

I really appreciate all of your help and advice!

green latch
#

You welcome!

dawn bolt
#

Take care!