i spent a long time trying to fix this probably somthing stupid but please help me.
i dont see any problem but its probably somthing stupid.

it compiles ok and when i run it it works until i get to the equations it prints somthing like
1.62111e-307 here is my code i dont know why but it posted when i pressed the space bar.

#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <fstream>

using namespace std;


int main()
{

          system("COLOR 5");
          system("TITLE tempature converter");
          
          double firstNumber, answer;
          char f, c, k, operation, repeat, form;
          char *tempatures;

          do
          {
          cout <<"\n\n\n\t \t \t \tINSTRUCTIONS \n \n First enter the number you will start with, then the form the number is in \n and lastly the form you want to convert to. \n \n c = celsius \n k = kalvin \n f = fahrenheit";

          cout <<"\n \n \n \n Enter the number you will start with: ";
          cin >> firstNumber;
          
          cout <<"\n Enter the form of the number you have (c/f/k): ";
          cin >> form;
          
          cout <<"\n Now enter c, f, or k for what you want to convert to: ";  
          cin >> operation;
            
                  if (form == 'f') {
                       if (operation == 'c'){
                          cout <<"\n The answer is: ";
                          answer == ((firstNumber - 32) * 5 / 9);
                          cout << answer;
                          }
                       if (operation == 'k'){           
                          cout <<"\n The answer is: ";
                          answer == ((firstNumber - 32) * 5/9 + 273.15);
                          cout << answer;
                          }
                       if (operation == 'f'){                                 
                          cout <<"\n The answer is: ";
                          answer == firstNumber;
                          cout << answer;
                          }
                  }
                  if (form == 'c') {
                       if (operation == 'c'){
                          cout <<"\n The answer is: ";
                          answer == firstNumber;
                          cout << answer;
                          }
                       if (operation == 'f'){
                          cout <<"\n The answer is: ";
                          answer == (firstNumber * 9 / 5 + 32);
                          cout << answer;
                          }
                       if (operation == 'k'){
                          cout <<"\n The answer is: ";
                          answer == (firstNumber + 273.15);
                          cout << answer;
                          }
                  }
                  if (form == 'k') {
                       if (operation == 'k'){
                          cout <<"\n The answer is: ";
                          answer == firstNumber;
                          cout << answer;
                          }
                       if (operation == 'c'){
                          cout <<"\n The answer is: ";
                          answer == (firstNumber - 273.15);
                          cout << answer;
                          }
                       if (operation == 'f'){
                          cout <<"\n The answer is: ";
                          answer ==((firstNumber - 273.15) * 9/5 + 32); //k - f
                          cout << answer;
                          }
                  
                  if (!c,f,k)   
                  cout <<"\n that wasn't c, f or k";
                  }
            ofstream tempature(tempatures, ios::app);
            tempature << form << operation << answer;

          cout <<"\n \n \n \t Do you want to convert another number (y,n): ";
          cin >> repeat;
          if (repeat == 'y'){
             system("CLS");
             }
          }
          while (repeat == 'y');
}

ps: i probably have some usless headers in there because of other stuff i was doing that didnt work.:)

Dani AI

Generated

Two quick things to check first (these are the most likely causes of the “weird number” and the compiler warnings pointed out): uninitialized variables and input/logic mistakes that leave expressions doing nothing. The “statement has no effect” warnings mean some lines aren’t changing program state the way you expect, so values like your result can remain indeterminate and print as garbage.

About the “newlines” question: pressing Enter leaves a newline character in the input buffer. Extraction with operator>> usually skips whitespace, but mixing different input methods (for example, getline or cin.get) or reading single characters can end up consuming that leftover newline instead of the intended input. Two common remedies are to discard leftover input with cin.ignore(...) or to skip leading whitespace when reading a char with a manipulator such as std::ws. See the reference docs for details: std::istream::ignore and std::ws.

Practical fixes to apply now:

  • Make sure you actually assign the computed value into your result variable (not leave the expression as a no-op).
  • Initialize variables before use (for example, set the result to 0.0 and any flags/char variables to sensible defaults). Uninitialized pointers (like an unused char * filename) and then using them to open a file cause undefined behavior; prefer std::string for filenames and construct std::ofstream with a valid string. See std::ofstream constructor.
  • Validate user input (use if/else or switch, and handle invalid entries) and prefer floating-point literals (e.g., 5.0/9.0) or ensure operands are doubles so division isn’t accidentally truncated.
  • Turn on compiler warnings (e.g., -Wall -Wextra) and fix each warning; they point directly at the problems.

Those steps will remove the garbage output and make the program behave predictably.

Recommended Answers

All 4 Replies

Let's see. In order to help you fix your code, you actually need to post your code.

sorry i didnt mean to post it just did when i pressed space bar idk why

Hopefully you saw warnings such as this:

main.cpp: In function `int main()':
main.cpp:38: warning: statement has no effect
main.cpp:43: warning: statement has no effect
main.cpp:48: warning: statement has no effect
main.cpp:55: warning: statement has no effect
main.cpp:60: warning: statement has no effect
main.cpp:65: warning: statement has no effect
main.cpp:72: warning: statement has no effect
main.cpp:77: warning: statement has no effect
main.cpp:82: warning: statement has no effect
main.cpp:86: warning: left-hand operand of comma has no effect
main.cpp:86: warning: right-hand operand of comma has no effect

If not, you may want to up the warning level in your compiler.

Here you do a comparison, not an assignment to answer:

answer == ((firstNumber - 32) * 5 / 9);

An assignment would be:

answer = ((firstNumber - 32) * 5 / 9);

And here

if (!c,f,k)

The comma usage probably is not what you mean to write.

[edit]And for the newlines left over after your character input, see this.

thanks for the help i just started c++ like a few days ago but what do you mean by newlines

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.