+ Reply to Thread
Results 1 to 9 of 9

Thread: Looking for basic C++ help

  1. #1
    The Playmaker
    Join Date
    Sep 2005
    Location
    Elkridge, MD
    Age
    29
    Posts
    3,276

    Default Looking for basic C++ help

    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;
    }

  2. #2
    The Special Teams Ace
    Join Date
    Dec 2005
    Location
    Centreville, VA
    Age
    29
    Posts
    326

    Default Re: Looking for basic C++ help

    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.
    Last edited by wantarace17; November-15th-2012 at 07:18 AM.

  3. #3

    Default Re: Looking for basic C++ help

    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.

  4. #4
    The Special Teams Ace
    Join Date
    Dec 2005
    Location
    Centreville, VA
    Age
    29
    Posts
    326

    Default Re: Looking for basic C++ help

    Quote Originally Posted by Sea Bass View Post
    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

  5. #5

    Default Re: Looking for basic C++ help

    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.

  6. #6
    The Special Teams Ace skins2323's Avatar
    Join Date
    Sep 2011
    Location
    Ellicott City, MD
    Posts
    320

    Default Re: Looking for basic C++ help

    are these posts in english?

  7. #7
    The Field Goal Team
    Join Date
    May 2007
    Location
    Behind the fridge
    Age
    49
    Posts
    476

    Default Re: Looking for basic C++ help

    Quote Originally Posted by skins2323 View Post
    are these posts in english?
    English isn't the only important language, you know.


  8. #8

    Default Re: Looking for basic C++ help

    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.
    “The definition of insanity is doing the same thing over and over again and expecting different results”.

    "There are no stupid people just stupid posts"

    Interested in learning about math, probability, or Computer Science and their connections to the real world? Learn more at my site: http://www.learninglover.com, or visit me on Twitter: @MindAfterMath

    A recent blog post : The Risk of Competition

  9. #9
    The Special Teams Ace
    Join Date
    Dec 2005
    Location
    Centreville, VA
    Age
    29
    Posts
    326

    Default Re: Looking for basic C++ help

    Quote Originally Posted by Thinking Skins View Post
    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. What BASIC changes would YOU make?
    By PROSCOUT in forum The Stadium
    Replies: 45
    Last Post: April-24th-2009, 01:41 AM
  2. Just graduated Basic trainin............
    By SkinsindaUSAF21 in forum The Stadium
    Replies: 48
    Last Post: December-29th-2006, 12:19 PM
  3. HELP: Visual Basic Questions
    By H-O-G in forum The Tailgate
    Replies: 3
    Last Post: September-28th-2006, 02:40 PM
  4. This sucks..... just found out i go to basic on Oct 3rd
    By SkinsindaUSAF21 in forum The Stadium
    Replies: 64
    Last Post: August-1st-2006, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts