char to vector problem

Discussion in 'C Programming' started by Carramba, Apr 15, 2005.

  1. Carramba

    Carramba Guest

    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.
    Carramba, Apr 15, 2005
    #1
    1. Advertising

  2. Carramba <> 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.
    Christopher Benson-Manica, Apr 15, 2005
    #2
    1. Advertising

  3. Carramba

    Carramba Guest

    On Fri, 15 Apr 2005 21:32:28 +0200, Christopher Benson-Manica
    <> wrote:

    > Carramba <> 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.
    Carramba, Apr 15, 2005
    #3
  4. Carramba <> 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.
    Christopher Benson-Manica, Apr 16, 2005
    #4
  5. Carramba

    Carramba Guest

    On Sat, 16 Apr 2005 01:04:34 +0200, Christopher Benson-Manica
    <> wrote:

    > Carramba <> 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 :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.
    Carramba, Apr 16, 2005
    #5
  6. On Fri, 15 Apr 2005 20:49:25 +0200, in comp.lang.c , Carramba
    <> 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 =----
    Mark McIntyre, Apr 16, 2005
    #6
  7. 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/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
    Emmanuel Delahaye, Apr 17, 2005
    #7
  8. 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++
    Emmanuel Delahaye, Apr 17, 2005
    #8
  9. (supersedes <>)

    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/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?"
    Emmanuel Delahaye, Apr 17, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,735
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  3. Replies:
    8
    Views:
    1,895
    Csaba
    Feb 18, 2006
  4. Replies:
    3
    Views:
    720
  5. Javier
    Replies:
    2
    Views:
    548
    James Kanze
    Sep 4, 2007
Loading...

Share This Page