Building a simple program

My name is Jon. I am a student taking C++ and I am an absolute beginner. I was asked to build a program that reads:

Develop a design that leads to an algorithm and a program that will
read in a number that represents the number of kilometers traveled. The
output will convert this number to miles. 1 kilometer = 0.621 miles. Call
this program kilotomiles.cpp.

So far I have built a program but I am getting errors.

This is what my code looks like. Any suggestions?



#include <iostream>

using namespace std;

int main()

{const double miles = 0.621;


//find total

const double total = kilos * miles;

//fIND OUT FROM THE USER HOW MANY KILOS THEY TRAVELLED.

cout<<"How many kilos did you travel?\n";


//show kilos in miles

cout<<"So you traveled" <<kilos * miles<<"miles today!\n"<<endl;

return 0;


}
Hi Jon. Please put your code inside code tags, as well as format it for us:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
  const double miles = 0.621;
  
  //find total

  const double total = kilos * miles;

  //fIND OUT FROM THE USER HOW MANY KILOS THEY TRAVELLED.

  cout<<"How many kilos did you travel?\n";

  //show kilos in miles

  cout<<"So you traveled" <<kilos * miles<<"miles today!\n"<<endl;

  return 0;
}


The error I get from this is "main.cpp:11:24: error: ‘kilos’ was not declared in this scope".

This means that on line 11 of the code I'm talking about something called 'kilos' that the compiler has never heard of.

In fact, in your code, it looks like you are trying to calculate the total miles before you know how many kilometers the user has traveled.

So move lines 9-11 to right before showing the total, and declare kilos before asking for it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
  const double miles = 0.621;

  //fIND OUT FROM THE USER HOW MANY KILOS THEY TRAVELLED.
  double kilos = 0;
  cout<<"How many kilos did you travel?\n";

  kilos = 1; // I'll let you figure out how to do console input

  //find total
  const double total = kilos * miles;

  //show kilos in miles
  cout<<"So you traveled" <<kilos * miles<<"miles today!\n"<<endl;

  return 0;
}


There are some touch up things in there, but at least now you have something you can work with.
Thanks! So how do I put the code inside code tags? And, would the format be source code?
code tags begin with [code] and end with [//code] ( but with only one slash ).

By formatting, I basically meant indentation. Look at the difference between your code and mine. I brought that first open brace up to the line right after main, took the declaration of miles off of the open brace, and indented each line by two spaces. Two spaces doesn't really matter, I just find it to look good on this site.
Topic archived. No new replies allowed.