Need Help with Programming Class: Convert Feet & Metres

In summary, the program is designed to allow the user to convert between feet and metres, depending on which choice they make. If conversion to metres is chosen, the user inputs feet, then the program finds metres. If conversion to feet is chosen, the user inputs metres, then the program finds feet. If neither choice is chosen by the user, then an error message is output.
  • #1
Desi23
7
0
I'm making a program for one of my programming classes and just needed some help!

#include <iostream>
#include <iomanip>

using namespace std;
int main()
{

int number;
float feet;
float metres;
float conv1;
float conv2;

cout << "Do you wish to convert 1. feet to metres or 2. metres to feet?" << "\n";
cout << "Enter your choice here:";
cin >>number;
cout <<"\n";


if (number==1)
{ cout << "Enter the distance in feet:";
cin >> feet;
cout <<"\n";

conv1= feet * 0.3048;

cout << setprecision(4) << fixed << feet << setw(8) << "feet is" << setw(7) << conv1 << setw(9) << "metre(s)" << "\n";

return 0;
}


else (number==2);
{ cout << "Enter the distance in metres:";
cin >> metres;
cout <<"\n";

conv2= metres * 3.2808;

cout << setprecision(4) << fixed << metres << setw(12) << "metre(s) is" << setw(7) << conv2 << setw(5) << "feet" << "\n";
}




return 0;










I have that so far, and I can't figure out how to write the code if somebody enters a number other then 1 or 2 it will output an "invalid" message. Can somebody help me out here this is driving me crazy lol.
 
Physics news on Phys.org
  • #2
Desi23 said:
I can't figure out how to write the code if somebody enters a number other then 1 or 2 it will output an "invalid" message.
I don't understand why you think the problem of writing code that does something if somebody enters a number other than 1 or 2 is any different than the problem of how to write code that does something if somebody enters the number 1.
 
  • #3
Oh, by the way:
else (number==2);​
makes no sense.

(Well, technically it has a well-defined meaning -- it's just that that meaning is certainly nothing you would ever even imagine writing in a program)
 
  • #4
Hurkyl said:
I don't understand why you think the problem of writing code that does something if somebody enters a number other than 1 or 2 is any different than the problem of how to write code that does some if somebody enters the number 1.

I've tried many ways so it outputs a INVALID message for example if I entered the number 3 where it says DO YOU WISH TO CONVERT 1...
But I cannot seem to get it working.
 
  • #5
Hurkyl said:
Oh, by the way:
else (number==2);​
makes no sense.

Care to elaborate?
How do I correct it then
 
  • #6
Desi23 said:
Care to elaborate?
How do I correct it then
What did you want the code to do?
 
  • #7
It's simple. You just need to use while loop.

Exp:

Code:
    int number;

    cout << "Do you wish to convert 1. feet to metres or 2. metres to feet?" << "\n";
    cout << "Enter your choice here:";
    cout << endl;

    while ((number != 1) && (number != 2))
    {
        cout << "Do you wish to convert 1. feet to metres or 2. metres to feet?" << "\n";
        cout << "Enter your choice here:";
        cout << endl;
    }

    cin >> number;
    return 0;
 
  • #8
ApexOfDE said:
It's simple. You just need to you while loop.
The program you wrote doesn't solve the problem the opening poster stated -- in fact, it surely doesn't even work how you intended. (And see the private message I sent you)
 
  • #9
Hurkyl said:
What did you want the code to do?

This program has to:
1. If conversion to meteres is chosen, then the user inputs the feet, then it finds the metres.
2. If conversion to feet is chosen, then te user inputs metres, then it finds the feet.
3. If neither choice is chosen by the user, then it must output an error message
I'm having trouble writing the code for number 3. I can't seem to write the correct if-else statements
 
  • #10
Im new in this forum so I don't know that rule. Sry about that. :(

About problem, I just think that he wants to display time-to-time a block of text when input is not 1 or 2; while loop can do that job.
 
  • #11
I've reformatted your code that reflects how it's structured, and added some colors to help identify certain statements. I've snipped out some code for brevity, and replaced it with comments.

Code:
if (number==1)
[color=green]    {  cout << "Enter the distance in feet:";
       cin >> feet;
       // ...
       return 0;
    }[/color]
else
    [color=red](number==2);[/color]

[color=purple]{  cout << "Enter the distance in metres:";
   cin >> metres;
   // ...
}[/color]

[color=brown]return 0;[/color]

In this piece of code, you have an if-else statement. The statement in green (an entire block) is executed if number==1 is true -- and the statement in red is executed if the number==1 is false.

(Yes, (number==2); is a statement. It doesn't do anything except in very esoteric circumstances, but it's a statement)

The statement in purple (an entire block) happens after the if-else statement. It doesn't get executed after the green block, because that contains a return statement which exits the function. However, it does get executed after the red block.

Finally, the brown statement is executed.
 
Last edited:
  • #12
Desi23 said:
2. If conversion to feet is chosen, then te user inputs metres, then it finds the feet.
Shouldn't you have an "if" statement to reflect that intent, then?


I'm having trouble writing the code for number 3. I can't seem to write the correct if-else statements
If you're having trouble with #3, don't make the common mistake of spending all of your time trying to figure out what's wrong with your #3 code. Sometimes, the mistake is elsewhere.
 
  • #13
Hurkyl said:
Shouldn't you have an "if" statement to reflect that intent, then?



If you're having trouble with #3, don't make the common mistake of spending all of your time trying to figure out what's wrong with your #3 code. Sometimes, the mistake is elsewhere.

Alright I read your previous post and I tried but now I'm getting this
error: expected primary-expression before "else"
error: expected ';' before "else"
 
  • #14
Well, it's hard to tell what your syntax error is if you don't post your code.

But surely, you have an "else" keyword such that the space between it and the previous "if" keyword does not consist of exactly one complete statement.

P.S. did you read my post describing what was wrong with your initial attempt?
P.P.S. I note the description of what you want to do doesn't have the word "else" in it. The direct translation of your description into a program wouldn't contain "else" either.
 
  • #15
bhhbog.jpg

thats what is happening, I am very confused
 
  • #16
Again, I'll reformat your code:
Code:
if(number == 1)
   {  cout << "Enter the distance in feet:";
      // ...
   }
else
   if (number == 2)
      [color=red];[/color]
[color=green]{  cout << "Enter the distance in metres:";
   // ...
}[/color]
else
   {  cout << "invalid";
   }

In particular, your final else doesn't correspond to any previous "if". I assume that's what the compiler is telling you when it says
error: expected primary-expression before "else"​
although one of the later errors may be more indicative.

The mistake is the semi-colon marked in red: you surely did not intend for that to be your "then-expression". Instead, you wanted the green block to be the "then-expression".
 
  • #17
Messing up if-statements in ways similar to this is a relatively common error. I suggest using a writing style that makes it difficult to make errors like that. The style I use looks like

Code:
if(test) {
   // stuff here
}

if(test) {
   // stuff here
} else {
   // stuff here
}

In this style, having a semi-colon in the wrong place looks really awkward, so it's easy to spot.



But, if you like your style -- then just be aware you're prone to this error, and look for it. (And learn how to tell when your compiler is complaining about that error)
 
  • #18
Hurkyl said:
Again, I'll reformat your code:
Code:
if(number == 1)
   {  cout << "Enter the distance in feet:";
      // ...
   }
else
   if (number == 2)
      [color=red];[/color]
[color=green]{  cout << "Enter the distance in metres:";
   // ...
}[/color]
else
   {  cout << "invalid";
   }

In particular, your final else doesn't correspond to any previous "if". I assume that's what the compiler is telling you when it says
error: expected primary-expression before "else"​
although one of the later errors may be more indicative.

The mistake is the semi-colon marked in red: you surely did not intend for that to be your "then-expression". Instead, you wanted the green block to be the "then-expression".

Ive worked it out! Many thanks! :biggrin:
 

FAQ: Need Help with Programming Class: Convert Feet & Metres

What is the purpose of the "Convert Feet & Metres" programming class?

The purpose of the "Convert Feet & Metres" programming class is to teach students how to write code that can convert measurements from feet to metres and vice versa. This is a common task in many programming projects and can be useful in fields such as engineering, construction, and science.

What programming language will be used in the "Convert Feet & Metres" class?

The specific programming language used in the "Convert Feet & Metres" class may vary depending on the course or instructor. However, the concept of converting measurements can be applied to any programming language, so the skills learned in this class can be transferred to other languages as well.

Do I need any prior programming experience to take the "Convert Feet & Metres" class?

Some knowledge of programming basics, such as variables, data types, and basic syntax, may be helpful in understanding the material covered in the "Convert Feet & Metres" class. However, the class may also be designed for beginners, so prior experience may not be necessary.

Will I learn how to convert other types of measurements besides feet and metres?

The "Convert Feet & Metres" class may focus specifically on feet and metres, but the skills learned can be applied to other types of measurements as well. The principles of converting one unit to another can be applied to any type of measurement, so the knowledge gained in this class can be useful in many different scenarios.

How can I apply the skills learned in the "Convert Feet & Metres" class in real-world projects?

The ability to convert measurements is a valuable skill in various industries, such as architecture, construction, and engineering. By learning how to write code that can convert feet to metres and vice versa, students can apply this knowledge in real-world projects to automate the process and save time and effort.

Similar threads

Replies
2
Views
3K
Replies
8
Views
1K
Replies
7
Views
1K
Replies
6
Views
3K
Replies
3
Views
1K
Replies
9
Views
3K
Back
Top