Jump to content
Washington Football Team Logo
Extremeskins

Looking for basic C++ help


EnFoRcEr_uPu

Recommended Posts

Hey Guys, my girlfriend is learning C++ in school and is having trouble. She's trying to write a program that will print/output just the first character from the 2nd line of a text file (text file seen below). This is what she currently has and while she can get it to print the first and second line of text, we're not sure of how to ignore the first line all together and print only the first character from the second. They're supposed to do this only with functions they've learned about in class so far (getline, ignore, cin...that's about it really). Can anyone tell us what she is missing?

[FILE]

4 4

3 1 2 1 b

1 1 1 1 a

C J N P 6

[PROGRAM]

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main () {

string line;

ifstream myfile;

myfile.open("problem_1.txt");

getline (myfile,line);

cout << line << endl;

cin.ignore();

myfile.close();

system("PAUSE");

return 0;

}

Link to comment
Share on other sites

ugh C++...let me look at this at work and see what I can figure out.

So after looking at it i kind of doesn't make sense based on the code she has written or has been given to her. Basically getline only gets 1 line. So the bad but totally workable solution would be to getline twice and then you'd only have the second line in your variable which you could then print.

In other words basically after the second getline, store the string in an array and print the first character from the array.

http://www.cplusplus.com/forum/beginner/15303/

Another way is to add add cout << line[0] after the second getline.

Link to comment
Share on other sites

Here's your code with the changes in bold:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main () {

string line;

ifstream myfile;

myfile.open("problem_1.txt");

getline (myfile,line);

getline (myfile,line);

cout << line[0] << endl;

cin.ignore();

myfile.close();

system("PAUSE");

return 0;

}

The result is that it prints out a 3.

The key points are that getline gets 1 line of the file so you need to call getline 2 times to get the 2nd line. And you can index a character in a string using [], so line[0] gets the first character, line[1] gets the 2nd, etc.

Link to comment
Share on other sites

Here's your code with the changes in bold:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main () {

string line;

ifstream myfile;

myfile.open("problem_1.txt");

getline (myfile,line);

getline (myfile,line);

cout << line[0] << endl;

cin.ignore();

myfile.close();

system("PAUSE");

return 0;

}

The result is that it prints out a 3.

The key points are that getline gets 1 line of the file so you need to call getline 2 times to get the 2nd line. And you can index a character in a string using [], so line[0] gets the first character, line[1] gets the 2nd, etc.

sweet glad we got it solved. :P...i only do this on the side

Link to comment
Share on other sites

Nevermind. My first answer works, but I think this is actually what they are looking for (changes in bold):

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main () {

string line;

ifstream myfile;

myfile.open("problem_1.txt");

getline (myfile,line);

char c = myfile.get();

cout << c << endl;

cin.ignore();

myfile.close();

system("PAUSE");

return 0;

}

In other words, they are trying to teach that getline() gets a line and get() gets a single character. getline() reads a line and moves you to the beginning of the 2nd line. From there you can just call get() to get the first character of the 2nd line. If you call get() again you will get the 2nd character, etc.

Link to comment
Share on other sites

just run the getline command twice. The first time it'll get the first line of the file, then since you don't want to display that line, run the getline command again.It'll erase the contents from that first line from memory and now the second line is in there.

---------- Post added November-15th-2012 at 09:47 AM ----------

Oh, and a good place to go for these type of questions is stackoverflow.com. Its another messageboard but its set up for people trying to learn CS stuff.

Link to comment
Share on other sites

just run the getline command twice. The first time it'll get the first line of the file, then since you don't want to display that line, run the getline command again.It'll erase the contents from that first line from memory and now the second line is in there.

---------- Post added November-15th-2012 at 09:47 AM ----------

Oh, and a good place to go for these type of questions is stackoverflow.com. Its another messageboard but its set up for people trying to learn CS stuff.

or Github

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...