Thread: a Quick Question

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    15

    Thumbs up a Quick Question (got the answer)

    im trying to understand why im forced to use 'cin.ignore();' in my example code
    and why when i run it without 'cin.ignore();' it jumps a bit to far in my code .
    its kinda anoying to not know. , pls explain to me

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    struct database
    {
        char name[256];
        char lastnume[256];
        char cellnr[256];
        char adress[256];
    };
    
    int main()
    {
        struct database data;
    
        cout<<" Please enter your data about you !\n";
        cout<<" Please enter your name:";
        cin>> data.name;
        cin.ignore();
        cout<<" Please enter your lastname:";
        cin.getline ( data.lastnume, 256, '\n');
        cout<<" Please enter your Cell nr.:";
        cin.getline ( data.cellnr, 256, '\n');
        cout<<" Please enter your adress:";
        cin.getline ( data.adress, 256, '\n');
    
        cout<<"\n Name:    \t"<< data.name;
        cout<<"\n Lastname: \t"<< data.lastnume;
        cout<<"\n Cell nr.:  \t"<< data.cellnr;
        cout<<"\n Adress:   \t"<< data.adress;
    
        cin.get();
    }
    sorry for my bad english
    Last edited by Flo; 06-25-2010 at 12:29 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Say you type in "Fred" and a newline
    Then "Flintstone" and a newline

    cin>> data.name;
    // This takes "Fred"

    cin.ignore();
    // This takes the newline

    cin.getline ( data.lastnume, 256, '\n');
    // This reads "Flintstone" and a newline


    Without the ignore(), the first getline call would immediately finish by reading the solitary newline left behind from your previous input.

    Ideally, you should pick just ONE input style (pick getline), and then you don't get into a mixup over who is responsible for processing which character(s).



    You have the same problem in C, if you mix scanf() with fgets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    got it !

    ty ty

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should also use std::string instead of chars.
    You can also drop the "struct" keyword when declaring a variable of the struct type, ie database data instead of struct database data.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    "struct" keyword i know, but its not wrong to let it there .. im new to coding so i let it be there to know what,were i done . coz that was an exercise to learn how to use structs.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's only going to be confusing. The struct keyword is used to declare a structure. Now your structure is a type and all variables are created by typing its type first, then the name.
    Not sure if you can do "class myclass myvar;".
    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.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    well i used "struct" keyword coz i get color blue on it and its easy to spot in code , so in small code were i mostly know what ive done that was a good ideea to spot but for long term that could do more harm then good ... geting bad habits is bad i know ty for pointing it for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM