Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is supposed to do this:
 Read the name of the file and try to open it, providing an error message if no file with the given name exists, as in a).
 Read the first line of the file to determine the value of N.
 Generate randomly an integer K in the range [1 .. N].
 Read the contents of the file until it reaches the K-th row (not counting the first row).
 Display the contents of that line on the screen.

For example:

Contents of a text file (ex: beatles.txt):
17
In the town where I was born
Lived a man who sailed to sea
And he told us of his life
In the land of submarines
So we sailed up to the sun
Till we found the sea of green
And we lived beneath the waves
In our yellow submarine
We all live in a yellow submarine
Yellow submarine, yellow submarine
And our friends are all on board
Many more of them live next door
And the band begins to play
As we live a life of ease
Everyone of us has all we need
Sky of blue and sea of green
In our yellow submarine
Execution result:
FILENAME? beatles.txt
SENTENCE: We all live in a yellow submarine // when K, randomly generated, was equal to 9

C++
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<cctype>
#include<vector>
#include <cmath>
#include <math.h>

using namespace std;


int main () {

  srand ((unsigned int) time(0));

  string filename;

  cout << "FILENAME? ";
  cin >> filename;

  ifstream fin(filename.c_str());

  if (fin.is_open())
  {cout << "SENTENCE: ";
  string line;
  char sline[256];
  std::string newline;

  vector<string> list;

	while(getline(fin, line))
	{

	  strcpy(sline, line.c_str());
	  int N;
      N = atoi(sline);
      int k;
      k = rand() % N + 1;



      for (int i=0; i<(k+1);i++)
          {
              getline(fin,newline);
          }}

cout << newline;

  fin.close();}

  else cout << "ERROR: File not found!";

  return 0;
}


Everytime I run the program it is returning:
FILENAME? beatles.txt
SENTENCE: Many more of them live next door
And the band begins to play
As we live a life of ease
Everyone of us has all we need
Sky of blue and sea of green
In our yellow submarine

I think it is a problem with the second getline but i don't know how to solve it.
Can you help?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-14 13:11pm    
What are line delimiters in the input file? By default you are using it's '\n', but you may actually have "\r\n" for Windows, or something else. Maybe you have different delimiters for different lines.
—SA
Nádia Carvalho 16-Apr-14 14:09pm    
I don't think so... I'm using a mac pro
Sergey Alexandrovich Kryukov 16-Apr-14 14:24pm    
What do you mean "think"? Just check it up.
On Mac OS X, this is CR.
—SA
Nádia Carvalho 16-Apr-14 15:28pm    
How can i check it?
Sergey Alexandrovich Kryukov 16-Apr-14 15:40pm    
What, you cannot read between lines (literally)? :-)
Any binary/hex editor/viewer. Or just correct code, bytewise reading.
—SA

1 solution

Remember that getline adds to the existing string. If you want to discard anything, you need to call newline.clear() before each call to getline.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900