reading more then 1 line of text in file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 13
Reputation: wtf??? is an unknown quantity at this point 
Solved Threads: 0
wtf??? wtf??? is offline Offline
Newbie Poster

reading more then 1 line of text in file

 
0
  #1
Jul 9th, 2008
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');
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: reading more then 1 line of text in file

 
1
  #2
Jul 9th, 2008
Use a loop to read more than one line:
  1. ifstream in ( "myfile" );
  2.  
  3. if ( in ) {
  4. string line;
  5.  
  6. while ( getline ( in, line ) )
  7. cout<< line <<'\n';
  8. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 882 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC