im new with c++ and dont know much but want to save the answer to the program i made when i do it names the file ÿÿÿÿ also the type of file is just called file how can i make it like .txt or somthing also just ignore the useless headers if there are any:)

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

Recommended Answers

All 6 Replies

The c-string temperatures is declared char *tempatures; , but never intialized. Then you try to use it in the constructor for temperature ofstream tempature(tempatures, ios::app); . You need to tell the compiler what file to open.

sorry this is probably a stupid question but how would i intialize it :S

tempatures = "file.txt";

This will output to the file "file.txt" (it will be created if it isn't already - thought i'm not sure if ios::app changes that behavior).

But this variable is unnecceasry. You could simply do:

ofstream tempature("file.txt", ios::app);

And get rid of the variable tempatures.

hey if you could help me more on how to take the data in the file and print it on the screen that would be nice but if not ill just do it myself thanks anyways

Say you have a file like this:
file.txt
45
hello
4

You could read and display the data like this:

ifstream fin("file.txt");
int x, y;
string str;

fin >> x >> str >> y;
fin.close()

cout << x << "\n" << y << "\n" << str << "\n";

This would output:
45
4
hello

thanks alot help me a bunch

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.