help with fgets

This is a discussion on help with fgets within the C++ Programming forums, part of the General Programming Boards category; Code: #include <conio.h> #include <iostream.h> struct movies { char name[50]; char director[50]; int rel; }; struct movies m1[20]; int main() ...

  1. #1
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62

    help with fgets

    Code:
    #include <conio.h>
    #include <iostream.h>
    
    struct movies
    {
        char name[50];
        char director[50];
        int rel;
    };
    
    struct movies m1[20];
    
    int main()
    {
        int i,n,view;
        do
        {
            cout<<"How many records do u want to input ? ";
            cin>>n;
        }while(n>20 || n<0);
        for(i=0;i<n;i++)
        {
            cout<<"\nEnter name : ";
            fgets(m1[i].name,50,stdin);
            cout<<"Enter the director's name : ";
            fgets(m1[i].director,50,stdin);
            cout<<"Enter release date : ";
            cin>>m1[i].rel;
        }
    
        cout<<"\n\nEnter the year you want to view : ";
        cin>>view;
    
        for(i=0;i<n;i++)
        {
            if(view==m1[i].rel)
            cout<<"\n"<<m1[i].name;
        }
    
        getch();
        return 0;
    }
    I can input both the strings. "Enter the name" and "Enter the director's name" get printed together in a row before i can input anything.... Plz run the program for details regarding what the problem is..
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  2. #2
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    and yes, i suppose i haven't followed the standards... sorry for that, i will surely do it next time after i get well acquainted with it
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    When you use cin to input an integer, it will leave the newline character in the input stream. The next time you call fgets() it will read up to that newline and assume you input an empty string. Try adding
    Code:
    cin.ignore();
    after each call where you input an integer.

    And since you already posted about it, I wont make a comment about your failure to write standard C++ code

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    You're mixing the methods of input and output. Both C I/O functions (like fgets()) and C++ iostreams are allowed to do things independently - most notably, buffer the output to the standard output devices. There is no guarantee they will be synchronised.

    Either use C I/O functions (eg fprintf(stdout ....) with fgets()) or C++ streams (eg cout and cin). Not both at once.


    [Yes - it is possible to use them together, but the techniques introduce even more opportunities for you to get yourself in trouble if you're not extremely careful, so I won't explain how].

    Unrelated to your problem : <conio.h> and getch() are not standard C or C++.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Don't use char, use std::string. They can easily be read using the >> operator of cin.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    @Elysia ^^!!

    Okay, then i am not supposed to use fgets in with C++ I/O streams... so what shall i do to input an string... just using cin doesn't help with string with spaces..

    btw, that was my first C++ code... I would like to know about the C++ standards if anyone is willing to help..
    Thx
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  7. #7
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    I am still searching over the net without any success... huh...
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    You can get use std::getline instead. It will read spaces (NOT std::cin.getline).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    error: no match for 'operator>>' in 'std::getline >> m1[i].movies::name'

    i replaced cin with std::getline and got that errror.. could you plz elaborate it... sry but never done c++ before this...


    i did some editing trying to follow the standards... Hope its better than last one..
    Code:
    #include <iostream>
    using namespace std;
    struct movies
    {
        std::string name;
        std::string director;
        int rel;
    };
    
    struct movies m1[20];
    
    int main()
    {
        int i,n,view;
        do
        {
            cout<<"How many records do u want to input ? ";
            cin>>n;
        }while(n>20 || n<0);
        for(i=0;i<n;i++)
        {
            cout<<"\nEnter name : ";
            std::getline>>m1[i].name;
            cout<<"Enter the director's name : ";
            std::getline>>m1[i].director;
            cout<<"Enter release date : ";
            cin>>m1[i].rel;
        }
    
        cout<<"\n\nEnter the year you want to view : ";
        cin>>view;
    
        for(i=0;i<n;i++)
        {
            if(view==m1[i].rel)
            cout<<"\n"<<m1[i].name<<"\n";
        }
    
        system("pause");
        return 0;
    }
    Last edited by n3cr0_l0rd; 06-07-2009 at 05:37 AM. Reason: removed the // used for disabling the codes and deleted the disabled codes
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    If you're trying to read a C-style string (array of char) getline() is a member function of the istream classes, not an object.

    If you wish to read a std::string from a string, then getline() is a function accepts two arguments by reference: the stream to be read from and the string to be read.

    Both forms of getline() can accept an additional (optional) argument that specifies the delimiter if you want reading to stop on something other than a newline.

    Either way, you can find out about them by reading your library documentation.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    You must learn to read documentation, because std::getline is not an object - it's a function!
    getline - C++ Reference
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    39
    here is how I would use a string with a struct;

    Code:
    struct exam
    {
        string name;
    };
    
    int main()
    {
        exam aStudent;
    
    // reading in..
       cout << "Enter a name" << endl;
       getline(cin, aStudent.name);
        
    
        //Print out...
    
        cout << "Exam blah..." << aStudent.name;
    
        return 0;
     }
    Last edited by ninety3gd; 06-07-2009 at 11:58 AM. Reason: code edit

  13. #13
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    Sorry but i didnt get how i referred getline as object ? My teachers suck at teaching... so could you plz explain it... seriously, you plz suggest me a FREE e-book so i can learn everything from scratch, just C++...
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by n3cr0_l0rd View Post
    Sorry but i didnt get how i referred getline as object ?
    Here, for instance:
    Code:
    std::getline>>m1[i].name;
    you are treating getline as something you can read from, as opposed to something that you do.

  15. #15
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    blame me for being dependent on teacher as he never said what :: and >>/<< acutually mean... any source i can learn all the basics of c++? Now i feel too much desperate for knowing nothing...
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21