-4

I have written a simple program to output different temperature units using conversions given. When I run the code on Visual Studios, it won't run due to build errors but no errors are shown. I have seen similar questions asked but couldn't really see any answer that solved my issue. I was also wondering whether I should be declaring the variables before or after the main function.

#include <iostream>

using namespace std;

int fahrenheit, i=0;
float celsius, absolute_value;

main() {
    cout.width(20);
    cout << "Fahrenheit" << "Celsius" << "Absolute value\n";
    for (i = 0; i = 15; i += 1) {
        fahrenheit = i * 20;
        celsius = ((fahrenheit - 30) * 5) / 9;
        absolute_value = celsius + 273.15;
        cout.width(20); /*each item in next output will have width of at least 20*/
        cout << fahrenheit << celsius << absolute_value;
    }
    system("pause");
    return 0;
}
jdog
  • 13
  • 5
  • 3
    *Visual Studios it won't run due to build errors but no errors are shown* That sentence does not make sense. – R Sahu May 28 '19 at 20:44
  • Do you get a build error, or do you get a run time exception? – NathanOliver May 28 '19 at 20:47
  • What are the errors? You are missing the includes for iostream and the return value for the main function – Thomas Sablik May 28 '19 at 20:48
  • 3
    `i = 15` is an assignment. – François Andrieux May 28 '19 at 20:50
  • 2
    `main` is required to have the return type `int` in c++. – François Andrieux May 28 '19 at 20:50
  • Include the return type for the main method to be `int main() {` and see what that does. – Nordii May 28 '19 at 20:50
  • @RSahu I have added a comma to clarify – jdog May 28 '19 at 20:54
  • @NathanOliver I got the standard: "There were build errors. Would you like to continue and run the last successful build" message. – jdog May 28 '19 at 21:00
  • @jdog, that doesn't still clarify what you are seeing in your code. Is there a compile time error? Is there a link time error? Is there a run time error? – R Sahu May 28 '19 at 21:00
  • 3
    @jdog Did you look at the "Output" tab, or the "Error list"? – Algirdas Preidžius May 28 '19 at 21:00
  • If you say no, down on the bottom, are there errors listed there in the build window? – NathanOliver May 28 '19 at 21:00
  • @ThomasSablik I have already included these – jdog May 28 '19 at 21:01
  • Switch the bottom pane from the Error List tab to the Output tab and see if the full build output gives you better diagnostics. – user4581301 May 28 '19 at 21:02
  • @RSahu "_That doesn't still clarify what you are seeing in your code._" This is the text, inside a message box, that you get, when you try to run the VS project, if such project has compilation/linking errors. – Algirdas Preidžius May 28 '19 at 21:02
  • @Nordii that's worked thanks. Why do I need to include int with main? – jdog May 28 '19 at 21:03
  • @FrançoisAndrieux thanks. that's happed helped as well. – jdog May 28 '19 at 21:04
  • @jdog 1) "_Why do I need to include int with main_" `main` is a function. Functions need to have return type. The return type of `main` is defined by the C++ standard. 2) Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), instead of coding randomly. – Algirdas Preidžius May 28 '19 at 21:04
  • @AlgirdasPreidžius Apologies. I now realise that was a stupid question. I had misread my example code. – jdog May 28 '19 at 21:06
  • Glad it worked. Posted explanation as an answer. – Nordii May 28 '19 at 21:13
  • @user4581301 How do I do this? – jdog May 28 '19 at 21:13
  • [This question is completely unrelated](https://stackoverflow.com/questions/31125153/activate-visual-studios-output-window-using-dte) but contains a picture showing the location of the output tab. – user4581301 May 28 '19 at 21:15
  • @Nordii The only problem is now the formatting is wrong. It's not producing a table as expected. – jdog May 28 '19 at 21:19
  • My answer was to get your code to compile and run. The next step is getting the right outputs. If you got your `for` loop working, don't forget to use spaces and newlines to adjust the formatting of your temperatures. – Nordii May 28 '19 at 21:28

3 Answers3

1
#include <iostream>

int main() {
    std::cout << "Fahrenheit\t" << "Celsius\t\t" << "Absolute value\n";
    for (int i = 0; i <= 15; i++) 
    {
        const int fahrenheit = i * 20;
        const float celsius = ((fahrenheit - 32.0) * 5) / 9;
        const float absolute_value = celsius + 273.15;
        std::cout << fahrenheit << "\t\t" << celsius << "\t\t"<< absolute_value << "\n";
    }
    system("pause");
    return 0;
}

Issues fixed:

  • main must return int
  • loop had assignement instead of comparison
  • variables should not be global
  • using namespace std; should be avoided
  • formula for fahrenheit to celsius was wrong
  • celsius was calculated with interger arithmetic despite being float
  • const correctness
  • cosmetic: formatted output with tabs
skeller
  • 1,151
  • 6
  • 6
  • Thanks a lot. Are you able to give an answer using a for loop and cout.width() as this is how we were told to do it in the lecture notes. Why do use const with these variables? So am I right in thinking that if Celsius is a float I much use 32.0 and not 32? – jdog May 28 '19 at 21:42
  • the 32.0 ensures you actually get the float result for celsius.you can use the cout.width if you want, nothing wrong with that. const is used because the variables are not changed (they are newly created on every run of the loop) – skeller May 28 '19 at 21:48
  • also you might want to have a look at the [c++ book guide](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – skeller May 28 '19 at 21:49
  • with 32 instead of 32.0 the celsius will be calculated as int converted to float after calculation during assignment, the .0 ensures floating point arithmetic is used on calculation – skeller May 28 '19 at 21:53
0

I fixed some coding errors. I think there are still some logical errors but now the code compile and program should run

#include <iostream>
using namespace std;

int main() {
    cout.width(20);
    cout << "Fahrenheit" << "Celsius" << "Absolute value\n";
    for (int i = 0; i <= 15; i += 1) {
        int fahrenheit = i * 20;
        float celsius = ((fahrenheit - 30) * 5) / 9;
        float absolute_value = celsius + 273.15;
        cout.width(20); /*each item in next output will have width of at least 20*/
        cout << fahrenheit << ' ' << celsius << ' ' << absolute_value << '\n';
    }
    system("pause");
    return 0;
}

You are missing the includes for iostream and the return type for the main function. I also moved the declaration of the global variables to where the variables are used the first time. The for loop was an endless loop. So I changed i = 15 to i <= 15. Maybe you wanted a loop condition like i < 15. I also added whitespaces and linebreaks between the values.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • 1
    Recommend marking and explaining the changes made. Are you sure you want `i <= 15` in the `for` loop? – user4581301 May 28 '19 at 21:10
  • 1
    @user4581301 _"Are you sure you want `i <= 15` in the `for` loop?"_ Doesn't do any harm IMO. – πάντα ῥεῖ May 28 '19 at 21:16
  • Please note that `cout.width(20);` only affects the *next* output, not "each item in next output". Also `fahrenheit` is an `int'... – Bob__ May 28 '19 at 21:18
  • @πάνταῥεῖ Not going to argue that. I worded the comment poorly poorly and should have recommended calling it out as a potential logic error. >= is an unusual thing to see in a `for` loop, and the unusual is usually a bug. – user4581301 May 28 '19 at 21:22
0

You need to add the int return type to the main signature.

In c++, the standard expects the main function to return an int, just as it expects the function to be called main. The return is most often used as an exit code, where 0 usually means successful execution without error.

Adding this in should allow your program to compile and run.

Nordii
  • 479
  • 1
  • 5
  • 15