getline in C?

This is a discussion on getline in C? within the C Programming forums, part of the General Programming Boards category; I could use cin but I would like to learn other ways of handling this. I have looked up the ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    getline in C?

    I could use cin but I would like to learn other ways of handling this. I have looked up the getline function but Im having problems implementing it for this program.

    Im hoping you people could give me some advice.

    This works:
    Code:
    void main ()
    {
    	char a[64];	
    
    	cout << "enter number\n";
    	while(notvalid(a))
    	{
    	cout << "a = ";
    	cin.getline(a,64);
    	}
    	getch();
    }
    This does not:
    Code:
    void main ()
    {
    	char a[64];	
    
    	printf("enter number\n");
    	while(notvalid(a))
    	{
    	printf("a = ");
    	scanf("%s", &a);
    	}
    	getch();
    }
    When I say "it works" or "it doesnt work", Im refering to entering a number and if the number is not valid then you must reinput the number. but when something like "-235 326.2" (notice the space) is entered scanf allows it?????

    Thanks,
    Ryan

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course scanf allows you to enter negative nubmers. You're using "%s". %s is for string values. A string value is basicly anything up to a space.

    You should really use something like fgets, combine with sscanf to validate your data.

    Additionally, since you can treat the name of an array as a pointer to its first element, then you do not need the & sign when scanning into an arrray.

    If you expect scanf to read a number, then use the correct format specifier, and stop reading strings.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest

    Exclamation I hope Im not cutting my own throat.

    Thanks for all your help on this board I really appreciate it and before I begin Im only asking/making some comments. Im not here to argue, fight, or anger anyone. I just want to say a few things on the silent n00bs behalf.

    SOME of us do read books, search boards, and are very interested in really learning this stuff we (SOME AGAIN) are not looking for you to do our homework, or write our whole program but sometimes we do get stuck on very basic things which you might take for granted. But all this is another story and I need to stop with that......

    Quzah, after you explained to me what I needed to search for I found some helpful posts which got me a step further (still didnt get it, but havent given up yet). During my search I now see why you are so frustrated. I think you answered my question about 1000 times. But the problem is that I didnt know to search for sscanf or fgets. Being so new, the problem some of us have is terminology and the knowledge of the functions.... If I search for what I think is the correct solution (ie getline) then what good is that going to do? Besides lead me down the wrong path again.

    Another problem I personally have (slam me if you want) but I learn much better from an example then trying to make sense out of the instructions such as: http://www.rt.com/man/fgets.3.html So does anyone know of a site many many examples in different situations? (ie. http://developer.irt.org/script/form.htm )

    Well I'll stop now before I get banned from the board.

    Again, Thank all of you for your help it is much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

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