-5

I have recently started to program in C++ and i wrote a simple file to try and test. It converts Celsius to Fahrenheit and vice versa. It keeps giving me an error about expecting a 'while', and a '('

As i said I'm new to this and really don't know what to try. I've moved the if else and else onto the same line as the ending curly brace, of the former if/if else statement.

#include <iostream>

int main()
{

    int f;
    int c;
    char choice;


        ask:do {
            std::cout << "Hello welcome! Would you like to convert from C to F (a) or F to C (b)?\n";
            std::cin >> choice;

        }


    if (choice == "a") { // that if before this comment has squiggly red line
        std::cout << "Great, What is the temperature in Celcius? (No decimals please)\n";
        std::cin >> c;

        f = (c * 1.8) + 32;

        std::cout << "The temp in Farenheight is " << f << "degrees\n";
    } 
    if else (choice == "b") {
        std::cout << "Great, What is the temperature in Celcius? (No decimals please)\n";
        std::cin >> c;

        c = (f / 1.8) - 32;

        std::cout << "The temp in Celsius is " << c << "degrees\n";
    } 
    else {
        std::cout << "Sorry that wasn't an option. Please try again.";
        goto ask;
    }


} // this also is squiggly

I would like it to output the number of c to f vice versa, but it wont run and it says:

expected a 'while' 18 ,5

expected a '(' 42 ,1

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 3
    You should check out [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/5910058) and spend a year or two reading.. – Jesper Juhl Jul 23 '19 at 22:04
  • @JesperJuhl It's not the amount of time in reading, but in practising. – Ripi2 Jul 23 '19 at 22:10
  • 1
    @Ripi2 Fine; Reading *and* practicing.. as if that wasn't obvious already.. – Jesper Juhl Jul 23 '19 at 22:13
  • BTW, double quotes is a string, single quotes is a character. You are comparing `choice`, which is a `char` to a string literal, such as `choice == "b"`. You should change this to `if (tolower(choice) == 'b')`. Note the use of single quotes. – Thomas Matthews Jul 23 '19 at 23:26
  • Compare your code to the example code from wherever you're learning from and find the differences. Things which might look minor to you such as swapping `if` and `else` matter. Also, messing with `goto` as a beginner is a recipe for [spaghetti code](https://en.wikipedia.org/wiki/Spaghetti_code). – eesiraed Jul 24 '19 at 04:08

2 Answers2

2

The code here has a syntax error:

    do {
        std::cout << "Hello welcome! Would you like to convert from C to F (a) or F to C (b)?\n";
        std::cin >> choice;

    }

The do keyword needs to be paired with a while, as the loop is do ... while rather than just do. So, for example, you might say something like

    do {
        std::cout << "Hello welcome! Would you like to convert from C to F (a) or F to C (b)?\n";
        std::cin >> choice;

    } while (!isValidInput(choice));

where isValidInput would be some helper function that validates the input.

So in other words, the issue isn't that you can't use an if statement here as much as your do ... while loop is incomplete.

There are some other syntax errors here as well. For example, you can't say if else, though else if is perfectly fine. Another issue: in C++, double-quotes are used for strings while single-quotes are used for characters, so comparisons of the form choice == "b" won't compile, because you're comparing a char and a string. Those errors will likely surface after you fix the aforementioned one.

As a final note, while goto is indeed legal in C++, it's generally frowned upon in favor of other options like regular styles of loop. If you follow the above approach of having your loop continue until you get a valid input, then you should be able to eliminate the goto that takes you back to the point where you ask for input.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

The do is a loop in c++. Specifically it is part of the do … while loop. It expects you to clarify how long the loop should be executed until the condition is met (and the do... while loop is different from while loop in that it executes the code at least once no matter what).

A ///

Your do is not enclosing your entire program in curly braces, so if you're not trying to loop your entire program then you may not need do at all. Simply make it

std::cout << "Hello welcome! Would you like to convert from C to F (a) or F to C (b)?\n"; std::cin >> choice;

B ///

If you want to loop the entire program the curly braces after do must capture everything and be finished with a while. In this scenario you can remove your goto statement; this answer can explain why you may want to avoid this What is wrong with using goto?.

E.g.

bool loop = true; do { // your program } while (loop); // this will loop while "loop" variable continues to be true

You can then have a condition in your loop somewhere that changes loop = false to break the loop, for example asking the user if they want to continue or not.

/// Some other things I noticed about your code:

char is assigned values with ' and note ".

if else is not correct, instead it is else if (…

Andrew Cina
  • 134
  • 10