char to vector problem

C

Carramba

hi!
I have strange (well to my enyway) problem the code below works like a
sweiz clock if it used for integers, but if I try to put char in it by
interacting with user, for loop ll doubble jump and insern char in
everyother place in vector. I'am guessing it some kind of problem becouse
when asking after char and press Enter I get 8 bits char en enter, is this
the issue? how to comme arround it?

I have tryed to use getchar() but it behavs in the sameway as scanf(),
Element 0 a
Element 1

Element 2 b
Element 3
Element 4 c

Element 5
Element 6 d

Element 7
iVektor1[0] = a
iVektor1[1] =

iVektor1[2] = b
iVektor1[3] =

iVektor1[4] = c
iVektor1[5] =

iVektor1[6] = d
iVektor1[7] =

a
b
c
d
****************getchar and scanf() exempel end*************************
getch() is doing allmost everything correct exept it suck chars in and you
can't se what you have enterd.
Element 0
Element 1
iVektor1[0] = a
iVektor1[1] = s

as
****************getch exempel end*************************
I allso have tryed getcha() it behaivse most 'normal' of them all in this
situation, exept after entering char it just stright to asking for next
element. like fits enter for you...

so back to my question, how can I fix so the user can enter char and press
enter before entering next char


void main(void){

int ctCount;
char/*if I use int works fine*/ iVektor1[8];

printf("Enter 8 characters folowed by Enter\n");
// Mata in element för element
for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("Element %d ",ctCount);
scanf("%c/*if I use %d works fine*/",&iVektor1[ctCount]);
}

for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("\niVektor1[%d] = %c/*if I use %d works fine*/",
ctCount,iVektor1[ctCount]);
}//slut for

iVektor1[8]='\0';
printf("%s",iVektor1);



--

Thanx in advance
and thanx for reading my post!

________________________
BTW. I know my english is not best in the word, so please stop bugging me
about my speling. And yes Iam sorry you don't understand what I mean, but
there is no point to yell at me. Have a nice day.
 
C

Christopher Benson-Manica

Carramba said:
I have strange (well to my enyway) problem the code below works like a
sweiz clock if it used for integers, but if I try to put char in it by
interacting with user, for loop ll doubble jump and insern char in
everyother place in vector. I'am guessing it some kind of problem becouse
when asking after char and press Enter I get 8 bits char en enter, is this
the issue? how to comme arround it?

Your question is misleading - vectors do not exist in C. Your
question, however, is topical...
void main(void){

Wrong:

http://www.eskimo.com/~scs/C-faq/q11.12.html
http://www.eskimo.com/~scs/C-faq/q11.14.html
http://www.eskimo.com/~scs/C-faq/q11.15.html
for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("Element %d ",ctCount);
scanf("%c/*if I use %d works fine*/",&iVektor1[ctCount]);
}

Unless I'm a moron (possible), this section of your code should still
work fine.
for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("\niVektor1[%d] = %c/*if I use %d works fine*/",
ctCount,iVektor1[ctCount]);
}//slut for
Ditto.

iVektor1[8]='\0';

Wrong - iVektor has 8 elements, so iVektor[8] does not exist.
 
C

Carramba

Your question is misleading - vectors do not exist in C.

The C Programming Language,
Brian W. Kernighan, Dennis M. Ritchie
http://cm.bell-labs.com/cm/cs/cbook/index.html

chapter 5 Pointer and vectors
do you want to say they are wrong?


Your
question, however, is topical...
void main(void){
Wrong:
http://www.eskimo.com/~scs/C-faq/q11.12.html
http://www.eskimo.com/~scs/C-faq/q11.14.html
http://www.eskimo.com/~scs/C-faq/q11.15.html

for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("Element %d ",ctCount);
scanf("%c/*if I use %d works
fine*/",&iVektor1[ctCount]);
}

Unless I'm a moron (possible), this section of your code should still
work fine.

shouda wouda.. if it was working fine I woudn't ask why it doesn't work.
for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("\niVektor1[%d] = %c/*if I use %d works fine*/",
ctCount,iVektor1[ctCount]);
}//slut for

Ditto.
Ditto to you to. :)
iVektor1[8]='\0';

Wrong - iVektor has 8 elements, so iVektor[8] does not exist.



--

Thanx in advance
________________________
BTW. I know my english is not best in the word, so please stop bugging me
about my speling. And yes Iam sorry you don't understand what I mean, but
there is no point to yell at me. Have a nice day.
 
C

Christopher Benson-Manica

Carramba said:
chapter 5 Pointer and vectors
do you want to say they are wrong?

This may be a translation error, but the proper title of the chapter
is "Pointers and Arrays".
Ditto to you to. :)

I've been known to be a moron before, so it wouldn't surprise me to be
one now...
Wrong - iVektor has 8 elements, so iVektor[8] does not exist.

Was this piece of information helpful? :)
 
C

Carramba

This may be a translation error, but the proper title of the chapter
is "Pointers and Arrays".
oh.. well ... you right after all.. . what is real difference.. they work
similary?!
I've been known to be a moron before, so it wouldn't surprise me to be
one now...
no need to by so hard on you self :) I'am sure you are very pleasent
person :p
Wrong - iVektor has 8 elements, so iVektor[8] does not exist.

yes ofcourse.. sorry for not pointing it out
but it didn't solve my problem with char to array ...
Was this piece of information helpful? :)



--

Thanx in advance
________________________
BTW. I know my english is not best in the word, so please stop bugging me
about my speling. And yes Iam sorry you don't understand what I mean, but
there is no point to yell at me. Have a nice day.
 
M

Mark McIntyre

for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("Element %d ",ctCount);
scanf("%c/*if I use %d works fine*/",&iVektor1[ctCount]);

scanf is a VERY bad function to use to read input. The FAQ covers some
reasons, but you've found one.

To enter a single character, you have to type TWO characters - for
example 'a' and a newline (the enter key). When you're using %c, the
first is read by your first loop through, and correctly loaded. The
second is then immediately read, loading '\n' into your 2nd array
element. And so on.
 
E

Emmanuel Delahaye

Carramba wrote on 15/04/05 :
The C Programming Language,
Brian W. Kernighan, Dennis M. Ritchie
http://cm.bell-labs.com/cm/cs/cbook/index.html

chapter 5 Pointer and vectors
do you want to say they are wrong?

On my French version of K&R2, I read "Pointeur et tableaux" that means
"Pointers and arrays"

http://vig.prenhall.com/catalog/academic/product/0,1144,0131103628-TOC,00.html

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

Carramba wrote on 15/04/05 :
void main(void){

int ctCount;
char/*if I use int works fine*/ iVektor1[8];

printf("Enter 8 characters folowed by Enter\n");
// Mata in element för element
for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("Element %d ",ctCount);
scanf("%c/*if I use %d works fine*/",&iVektor1[ctCount]);
}

for(ctCount = 0 ; ctCount < 8 ; ctCount++){
printf("\niVektor1[%d] = %c/*if I use %d works fine*/",
ctCount,iVektor1[ctCount]);
}//slut for

iVektor1[8]='\0';
printf("%s",iVektor1);

You make it too complicated. C programmers like simple things...

#include <stdio.h>

int main (void)
{
char arr[8];
int i;

printf ("Enter 8 characters followed by <enter>\n");

for (i = 0; i < 8; i++)
{
scanf ("%c", arr+i);
printf ("Element %d = '%c'\n", i, arr);
}

for (i = 0; i < 8; i++)
{
printf ("arr[%d] = %c\n", i, arr);
}

arr[7] = 0;
printf ("'%s'\n", arr);
return 0;
}

Enter 8 characters followed by <enter>
123456789
Element 0 = '1'
Element 1 = '2'
Element 2 = '3'
Element 3 = '4'
Element 4 = '5'
Element 5 = '6'
Element 6 = '7'
Element 7 = '8'
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
arr[6] = 7
arr[7] = 8
'1234567'

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

Carramba wrote on 15/04/05 :
The C Programming Language,
Brian W. Kernighan, Dennis M. Ritchie
http://cm.bell-labs.com/cm/cs/cbook/index.html

chapter 5 Pointer and vectors
do you want to say they are wrong?

On my French version of K&R2, I read "Pointeurs et tableaux" that means
"Pointers and arrays"

http://vig.prenhall.com/catalog/academic/product/0,1144,0131103628-TOC,00.html

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top