dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Can't figure this out- desperate for help C++ Loops and data files.

  1. #1
    Join Date
    Nov 2018
    Posts
    7

    Can't figure this out- desperate for help C++ Loops and data files.

    Code:
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main() {
        ifstream inputFile;
        string fileName, name, first, last, line;
        int selection=0,students;
        
        cout<<"How would you like to read in students names?\n";
        cout<<"(1) Entering names from the keyboard?\n";
        cout<<"(2) Reading Names from an external file?\n";
        cout<<"(3) End program\n";
        cin>> selection;
    
        switch (selection) {
            case 1:
                cout<<"Enter number of students(must be between 5 and 30 names) ";
                cin>> students;
                
                while  (!(students>=5 && students<=30))
                {
                    cout<<"Invalid, please enter a number between 5 and 30 ";
                    cin>> students;
                }
    
                cin.ignore();
                
                for (int count = 1; count<=students ; count++)
                {
                    cout<<"Enter students name " <<count<< ": ";
                    getline (cin, name);
                    
                    if (count==1)
                    {
                         first = name;
                         last = name;
                    }
    
                    if (name < first)
                        first = name;
    
                    if (name > last)
                        last = name;
                }
                
                cout<<"Student at the top of the list is "<<first<<endl;
                cout<<"Student at the end of the list is "<<last<<endl;
                
                break;
            case 2:
                cout<<"Enter file name: ";
                cin.ignore();
                getline(cin,fileName);
                
                inputFile.open(fileName);
                if (inputFile) {
                     while(getline(inputFile >> name)) {
                        cout<< name << endl;
    
                        if(name <first)
                            first = name;
    
                         if(name > last)
                             last = name;
                   
                     }
                }
                else
                {
                   cout<<"Error opening the file.\n";
                }
                        
                cout<<"Student at the top of the list is "<<first<<endl;
                cout<<"Student at the end of the list is "<<last<<endl;
                        
                break;
    
            case 3:
                cout<< "End program.";
                
                cin.ignore(256, '\n');
                cin.get();
                
            default: 
                cout<<"You did not enter 1, 2 or 3.";
                break;
        }
    }
    The red line is my issue." It says No matching function for call to 'getline' "
    I am also having a hard time getting the code to read the input file line by line. It keeps outputting word by word. A little help would be great!
    Last edited by mary_um; November 16th, 2018 at 09:19 AM. Reason: Added code tags

  2. #2
    Join Date
    Nov 2018
    Posts
    7

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    Code:
    inputFile.open(fileName);
    if (inputFile) {
    while(getline(inputFile>> name))
    cout<< name << endl;
    this is where my issue is. the text did not turn red like I intended :/
    Last edited by 2kaud; November 16th, 2018 at 08:44 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    I've fixed your red line issue in post #1. When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and lick '#'.

    Code:
    while(getline(inputFile>> name))
    should be
    Code:
    while (getline(inputFile, name))
    as getline() takes 2 parameters. The first the stream and second the string. See http://www.cplusplus.com/reference/s...tring/getline/
    Last edited by 2kaud; November 16th, 2018 at 08:51 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    to read the input file line by line. It keeps outputting word by word.
    There are basically 2 ways of obtaining text data from a file. By reading the whole line using getline() or by reading the next element using stream extraction (>>). getline() reads the contents into the specified string variable until a \n (newline) is found (or other delimiter if one is specified). The \n is discarded. >> obtains the next element from the stream. It first discards any white-space chars (space, tab, \n, \r) and then reads the contents until another white-space char is found. This terminating white-space char is not discarded. This difference between >> and getline() is the reason you have .ignore() in your code so that the terminating \n left by >> is extracted and ignored before getline() is used - otherwise getline() sees the \n left by >> and returns without reading anything! If your line of text consists of multiple words separated by space(s) then getline() will read the whole line but >> will only read a word upto the next space (or \n).
    Last edited by 2kaud; November 16th, 2018 at 09:27 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  5. #5
    Join Date
    Nov 2018
    Posts
    7

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    Thank you for the help (and posting advice). I really apprecaite it!

  6. #6
    Join Date
    Nov 2018
    Posts
    7

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    Maybe you can help me with one more thing. I have been messing with the code how a while and I did at one point get it to read the file and give me the output I needed, which is two names, from the list, the one that should be at the top and the one that should be on the bottom, alphabetically. But now it's not working and I haven't changed the if statements from when it was, any ideas?

    Code:
    case 2:
               cout<<"Enter file name: ";
               cin.ignore();
               getline(cin,fileName);
               inputFile.open(fileName);
               if (inputFile.is_open())
               {
                    while( getline (inputFile , fileName))
                    {
                       cout<< fileName << endl;
    
                       if(fileName < first)
                           first = fileName;
    
                       else if(name > last)
                           last = fileName;
                   }
    
               }
               else
               {
                        cout<<"Error opening the file.\n";
               }
    
                inputFile.close();
    
                cout<<"Student at the top of the list is "<<first<<endl;
                cout<<"Student at the end of the list is "<<last<<endl;
    
                break;
    (also, not seeing where to select "formatted code", sorry I'm a mess.
    Last edited by 2kaud; November 16th, 2018 at 03:56 PM.

  7. #7
    Join Date
    Nov 2018
    Posts
    7

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    Disregard! I figured it out!!

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: Can't figure this out- desperate for help C++ Loops and data files.

    Quote Originally Posted by mary_um View Post
    Disregard! I figured it out!!
    Great!

    (also, not seeing where to select "formatted code")
    Before pasting in your code, it should be formatted in your source editor. Then you click Go Advanced, select the inserted pasted formatted code and click '#'.

    In your source editor select the formatted code as you would in any other application, then ctrl-c to copy. Then in the thread into which you want to paste the code, then ctrl-v to paste - as you would to copy/paste text between any 2 applications.
    Last edited by 2kaud; November 16th, 2018 at 04:02 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)