C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 29th, 2008, 12:49 AM
ratedmax ratedmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 4 ratedmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 35 sec
Reputation Power: 0
"stuck help" read and print file result Again

// This program reads the number of lines in the file
// and the number of interger values (not integer digits)

this is my input file i called it "ch_4file.txt"

19 22
15 12
18 34
10 12
15 55
23 10


the program its working the first part at least . its reading the numbers of lines in the file, (thats the first part of the program) but im stuck trying to get it to read how many integer values are in the file. its the second part im stuck on, anyone help please... Im new at writing programs
im not sure if im doing it right.




#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
char c;
int num_lines(1);
ifstream input_1;

input_1.open("ch_4file.txt");
if(!input_1.fail())

{ do
{ c = input_1.get();
if (c == '\n') num_lines++;
} while (!input_1.eof());

cout << "There are " << num_lines << " lines in the file" << endl;

}

else
{ cout << "Error opening input file." << endl;
};

{
int sum(0);
int max_int;

ifstream input_1;

input_1.open("ch_4file.txt");
input_1 >> max_int;

for (int i=1; i<=max_int; i++)
{
sum+=i;
};

cout << "There are " << max_int << " interger values. "<<endl;

}

system("PAUSE");
return EXIT_SUCCESS;
};

Last edited by ratedmax : March 2nd, 2008 at 12:06 AM. Reason: rephrase the question

Reply With Quote
  #2  
Old March 1st, 2008, 05:48 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Unhappy

(Your original: )
cpp Code:
Original - cpp Code
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7.     char c;
  8.     int num_lines(1);
  9.     ifstream input_1;
  10.  
  11.     input_1.open("ch_4file.txt");
  12.     if(!input_1.fail())
  13.    
  14.     {  do
  15.          {  c = input_1.get();   
  16.         if (c == '\n') num_lines++; 
  17.          } while (!input_1.eof());
  18.          
  19.     cout << "There are " <<  num_lines << " lines in the file" << endl;
  20.  
  21.     } 
  22.    
  23.     else
  24.         {  cout << "Error opening input file." << endl;
  25.         };
  26.      
  27. {
  28.     int sum(0);
  29.     int max_int;
  30.    
  31.      ifstream input_1;
  32.    
  33.      input_1.open("ch_4file.txt");
  34.      input_1 >> max_int;
  35.    
  36.      for (int i=1; i<=max_int; i++)
  37.      {   
  38.          sum+=i;
  39.       };
  40.      
  41.      cout << "and  " << max_int << " interger values. " <<endl;
  42.      
  43. }
  44.  
  45.     system("PAUSE");
  46.     return EXIT_SUCCESS;
  47. };


Why do you not check if the file opened successfully the second time? But instead of opening it a second time, use input_1.seekg()

Quote:
i got the program to work.but ...
its reading the numbers of lines in the file, but im stuck
trying to get it to read how many integer values are in the file. its the second part im stuck anyone help please...


Messy indentation :/
Is that supposed to mean stuck on reading the integers themselves? As in calculating the sum of them?

Code:
//make sure you are reading from the beginning of the numbers.
for (int i=1; i<=max_int; i++)
{	
	int this_int;
	input_1 >> this_int;
	if (input_1.fail()) { 
		cerr << "Encountered letters where I expected numbers!"; 
		return EXIT_FAILURE;
	}
	sum+=this_int;
};
// The first number (max_int) being wrong is still unaccounted for with this code.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > "stuck help" read and print file context


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap