could someone show me how to read more then 1 line of text in a file and print it on the screen im kinda new at this tho i no how to do 1 line

using namespace std;

int main()
{

          system("COLOR 5");
          system("TITLE tempature converter");
          
          double dfirstNumber, danswer, firstNumber, answer;
          char f, c, k, operation, repeat, form, dform, doperation;

          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. this program also saves your what you do. to access this type open now \n \n c = celsius \n k = kalvin \n f = fahrenheit";
          cin >> fileopen
               if (fileopen == open){
                  ifstream fin ("data.txt");
                  fin >> dform >> doperation >> danswer;
                  fin.close()
                  cout << dfirstNumber << dform << danswer << doperation;
                  }

          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("data.txt", ios::app);
            tempature << firstNumber << " " << form << " " << answer << " " << operation << "\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');
}

Dani AI

Generated

A few practical points based on original code and suggestion.

If the file contains simple, whitespace-separated records (your program writes: number form answer operation), the most robust approach is to read those fields in a loop using formatted extraction. Example:

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream fin("data.txt");
    if (!fin) { std::cerr << "Could not open data.txt\n"; return 1; }

    double storedNumber, storedAnswer;
    char storedForm, storedOp;
    while (fin >> storedNumber >> storedForm >> storedAnswer >> storedOp) {
        std::cout << storedNumber << ' ' << storedForm << ' '
                  << storedAnswer << ' ' << storedOp << '\n';
    }
}

Use getline (as suggested) when you need the entire line including spaces; then parse that line (for example with std::istringstream) if it contains multiple fields.

Concrete fixes and pitfalls visible in the posted code:

  • Add the proper headers (<iostream>, <fstream>, <string>, <sstream> when needed).
  • Ensure semicolons after statements (cin >> fileopen;, fin.close();).
  • Compare strings correctly: if (fileopen == "open")open without quotes is not a string.
  • The test if (!c,f,k) is invalid. Use if (form != 'c' && form != 'f' && form != 'k').
  • Always check the file opened successfully before reading.
  • Keep file read order consistent with how you wrote the file (number then form then answer then operation).

If records might contain spaces or optional fields, read full lines with getline and then split/parses each line. Also consider separating UI logic from file I/O: write small functions for reading/writing records to reduce mistakes.

Use a loop to read more than one line:

ifstream in ( "myfile" );

if ( in ) {
  string line;

  while ( getline ( in, line ) )
    cout<< line <<'\n';
}
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.