Velocity Reviews > char to vector problem

# char to vector problem

Carramba
Guest
Posts: n/a

 04-15-2005
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.

Christopher Benson-Manica
Guest
Posts: n/a

 04-15-2005
Carramba <(E-Mail Removed)> spoke thus:

> 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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Carramba
Guest
Posts: n/a

 04-15-2005
On Fri, 15 Apr 2005 21:32:28 +0200, Christopher Benson-Manica
<(E-Mail Removed)> wrote:

> Carramba <(E-Mail Removed)> spoke thus:
>
>> 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.

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.

Christopher Benson-Manica
Guest
Posts: n/a

 04-15-2005
Carramba <(E-Mail Removed)> spoke thus:

> 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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Carramba
Guest
Posts: n/a

 04-16-2005
On Sat, 16 Apr 2005 01:04:34 +0200, Christopher Benson-Manica
<(E-Mail Removed)> wrote:

> Carramba <(E-Mail Removed)> spoke thus:
>
>> 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".

oh.. well ... you right after all.. . what is real difference.. they work
similary?!

>> Ditto to you to.

>
> 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

>> > 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.

Mark McIntyre
Guest
Posts: n/a

 04-16-2005
On Fri, 15 Apr 2005 20:49:25 +0200, in comp.lang.c , Carramba
<(E-Mail Removed)> wrote:

> 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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Emmanuel Delahaye
Guest
Posts: n/a

 04-17-2005
Carramba wrote on 15/04/05 :
>> 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?

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

http://vig.prenhall.com/catalog/acad...28-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

Emmanuel Delahaye
Guest
Posts: n/a

 04-17-2005
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[i]);
}

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

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++

Emmanuel Delahaye
Guest
Posts: n/a

 04-17-2005
(supersedes <(E-Mail Removed)>)

Carramba wrote on 15/04/05 :
>> 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?

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

http://vig.prenhall.com/catalog/acad...28-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?"

 Thread Tools

 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 On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM wwj C++ 7 11-05-2003 12:59 AM

Advertisments