LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2005, 03:38 PM   #16
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15

are you saying thet the '\n' is from when i pressed enter after writing the actual name of the program (such as ./program) ??
 
Old 08-22-2005, 04:02 PM   #17
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I don't think so, I guess it's the one you entered to trigger the scanf.
 
Old 08-22-2005, 04:09 PM   #18
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
No


ok this has to do with the keyboard being buffered.


when getchar is called it waits for an enter to confirm the selection but that enter generates
another character in the buffer "\n" ok now if you have more then one getchar or you are implementing this in a loop then that \n will be used as the next input method instead of the correct one. You have 2 solutions to this either create a input method that doesn't
suffer from this deficiency or flush the buffers after every input.
 
Old 08-22-2005, 04:16 PM   #19
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15
'\n'

okay, i get what you mean, but i dont see why getchar would do this. this getchar function gets a single character. this will naturally have to be followed by <enter> to confirm what was typed. surely getchar takes this into account?

secondly: i didnt use getchar in the program, i used scanf("%c");
any ideas?
 
Old 08-22-2005, 04:38 PM   #20
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15
yes but it asks me to enter the second one before ive even entered the first. there can be no enter in the buffer other than that which i used to start the program ('./program'). if it was buffereing an '\n' than it would be the second, not the first getchar that would be skipped, would it not?
(the above is out of curiosity as, im not using getchar, im using scanf("%c"); but i guess this wouldnt change things.
 
Old 08-22-2005, 04:55 PM   #21
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Post your full source please and then we can see whats wrong.
 
Old 08-22-2005, 05:02 PM   #22
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
If you're using scanf("%c",&somechar);, it will read ONE character in from the buffer. Perhaps you entered a number to tell it the number of numbers, as your pseudocode suggested? For example (input in bold):
Code:
Number of numbers: 5 (NEWLINE)
Reads 5 in, and leaves the newline in the buffer.
 
Old 08-22-2005, 09:47 PM   #23
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
if it was buffereing an '\n' than it would be the second, not the first getchar that would be skipped, would it not?
The first \n, the one following the command is gotten by the shell, not your program.
Quote:
but i dont see why getchar would do this. this getchar function gets a single character. this will naturally have to be followed by <enter> to confirm what was typed. surely getchar takes this into account?
This is wrong, enter is not mandatory to confirm something was typed, at least from a program point of view.
You can have yours reading characters without needing \n by turning the interface in "raw" mode, like "vi" do for example.

Wrap your program with a shell script like this:
Code:
#!/bin/sh
stty raw
./program
stty -raw
This also can be done in plain C, but the former is simpler, at least to test the "uncooked" mode.
 
Old 08-26-2005, 06:50 PM   #24
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15
code:

sorry for the long delay. as i havnt ever got feedback on code, as well as an answer as to what the fault is, i wouldnt mind your opinions on wether this code would be considered neat/clean/efficient etc.#include <stdio.h>
main()
{
int a, b, c;
char *d;
printf("enter amount of values: ");
scanf("%d", &a);
d = (char*) calloc(a, sizeof(char));
if (d == NULL)
printf("cannot allocate memory.\n");
else {
for (b=0;b<=a-1;b++)
{ printf("please enter char %d: ", b+1);
scanf ("%c", d+b);
}
for (b=0;b<=a-1;b++)
{ if ( *(d+b) == ' ')
*(d+b) = '_';
}
printf( "you entered: ");
for (c=0;c<=a-1;c++)
printf("%c ", *(d+c));
printf("\n");
}
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Divide up lines with string delimiter elmu Programming 3 10-07-2005 08:48 AM
Using a seperate router to divide network coindood Linux - Networking 4 09-14-2005 11:13 AM
how to divide two string variables user_linux Programming 3 06-20-2005 04:54 PM
Partimage & Divide by Zero KimVette Linux - Hardware 3 06-01-2005 10:18 AM
Divide a file into 2 parts bmbsa Linux - Newbie 3 08-13-2003 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration