Solved

# scanf question...i guess

Posted on 2003-02-18
Medium Priority
268 Views
How do i use scanf to read a line one character at a time and then make it stop reading when it hits a newline?  Also, lets say i use scanf("%c",&ch), if i wanted to put each character into an array, do I use like a for loop to do it?  or any other suggestions?
0
Question by:deta_gen
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 3
• +3

LVL 9

Expert Comment

ID: 7978933
one posibility is

char c;
do
{
scanf( "%c", &c );
doWhatEver(c);
}while( c!='\n')

otherwise if conio.h can be used

(if i'm not mistaken)
char c;
do
{
c = (char)getch();
doWhatEver(c);
}while( c!='\n')
0

LVL 8

Accepted Solution

akshayxx earned 136 total points
ID: 7978991
to read the string of characters .. and fill them up in an array .. till the newline
do this

char arr[SIZE];

scanf("%[^\n]",arr);

this will read all the characters uptil newline ( excluding it) .. in the array arr.

to use a for loop with scanf .. to read character by character .. isnt suggestible..
0

LVL 8

Assisted Solution

Exceter earned 132 total points
ID: 7979004
Although it does not use scanf() another possiblity is,

char st[100];
int count = 0;

while(1)
{
st[count] = getch();
count++;
if( st[count-1] == '\n' )
break;
}

st[count] = '\0';

The main advantage of this cource of action is that you do not have to press enter after each character.

Exceter
0

LVL 8

Expert Comment

ID: 7979006
>> The main advantage of this cource of action is that you do not have to press enter after each character.

As you do when you put scanf() into a loop. :-)

Exceter
0

LVL 8

Expert Comment

ID: 7979015
u can also use fgets(arr,SIZE,stdin);

the difference being .. in fgets u 'll get newline character also read.. while in above scanf solution u will have all the character till newline .. but not the newline itself..

and both of them will ensure that u have to press enter only once
:)
0

LVL 1

Assisted Solution

Gula earned 132 total points
ID: 7979158
I like the way akshayxx proposed, I always use it, in addition to that I add a number after % in order not to exceed the lenght of the array, and white space before % like this:

char arr[80];

scanf(" %79[^\n]",arr);
0

LVL 8

Expert Comment

ID: 7979188
>> I like the way akshayxx proposed

Agreed.
0

LVL 1

Expert Comment

ID: 7979280
sorry for interrupting everyone,
akshayxx can you check out the lounge, please
sorry again thanks
0

LVL 9

Expert Comment

ID: 7979297
i agree with you both

i forgot about storing it in an array =(

sorryp
0

LVL 8

Expert Comment

ID: 7979446
Gula : which lounge u want me to check .. i couldnt get exactly what u meant..
0

LVL 1

Expert Comment

ID: 7979630
akshayxx : Miscellaneous -> Lounge... in the menu on the left... I have some questions for you
0

Author Comment

ID: 7979753
The function is going to call something like
get_line(a)
and
get_line(b)

How do i make it so that what ever the get_line calls...for example, get_line(a), the function will do the scanf and put it into the array a[] ..since get_line(a) was specified

void get_line(int *a)
{
char temp;
char t[26];
char t2[26];
temp = &a;
if *temp =='a' {

scanf("%[^\n]",t);

}
if *temp =='b' {

scanf("%[^\n]",t2);

}
0

LVL 1

Expert Comment

ID: 7981703
If I understood you right, you have to make something similar to gets, then I would suggest you go with Exceter's solution... the third from the top, like

void get_line(char a[]){
int count = 0;

while(1)
{
a[count] = getch();
count++;
if( a[count-1] == '\n' )
break;
}

a[count] = '\0';
}

I'm not sure what you were trying to do in your code,
you are passing the array, so why are you creating two other ones?  and why int* a, if you want to read chars? temp is not a pointer so you cannot use *temp, and why to compare to 'a' and 'b'? you want to check exaustively every character? If you still didn't get your solution, please explain again..
0

LVL 20

Expert Comment

ID: 10004293
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between akshayxx, Exceter, and Gula.

jmcg
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll