a problem with strings 
Author Message
 a problem with strings

Hi everybody!

I am a new newbie. This is a menu program, I want user to choose one
character and display the choice .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)

{
  char a[] = "Deposit";
  char b[] = "Withdrawal";
  char c[] = "Cancel";

  printf("CHOOSE ONE:\n");
  printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
  scanf("%s.\n%", &a);
  printf("You select %s\n", a, b, c);

  printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
  scanf("%s.\n%", &a);
  printf("You select %s\n", a, b, c);

  printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
  scanf("%s.\n%", &a);
  printf("You select %s\n", a, b, c);

  system("PAUSE");
  return 0;

Quote:
}

But when I test the program it looks like that:
CHOOSE ONE:
 a Deposit.
 b Withdrawal.
 c Cancel.
a
You select a
CHOOSE ONE:
 a a.
 b Withdrawal.
 c Cancel.
b
You select b
CHOOSE ONE:
 a b.
 b Withdrawal.
 c Cancel.
c
You select c

Please help, where is the mistake!
Thak you,
Anna



Wed, 24 Nov 2004 08:58:17 GMT  
 a problem with strings

Quote:

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> int main(void)

> {
>   char a[] = "Deposit";

Here you put the string "Deposit" into a.

Quote:
>   char b[] = "Withdrawal";
>   char c[] = "Cancel";

>   printf("CHOOSE ONE:\n");
>   printf(" a %s.\n b %s.\n c %s.\n", a, b, c);

Here you print a, which contains "Deposit".

Quote:
>   scanf("%s.\n%", &a);

This scanf call is stuffed, for one thing there is an unterminated %
sequence at the end. You do not need the & when you're passing a pointer to
a string. In fact it will send the wrong type of pointer, but that's not
the problem that you're seeing. Also the array a is only enough space to
store a 7-character string, so if the user enters more than 7 characters
you will be in trouble.

But the main problem is that here you read a new string into a, which
overwrites the previous contents "Deposit".

Quote:
>   printf("You select %s\n", a, b, c);

Here you are only printing out one string, so you should not be passing b
or c into the printf.

Quote:
>   printf(" a %s.\n b %s.\n c %s.\n", a, b, c);

And here's the problem you see: the array a has been overwritten with the
string that you entered, so it no longer contains the prompt string
"Deposit".

The way to fix this is to always use fgets to read strings:

#include <stdio.h>

int main(void)
{
  char a[] = "Deposit";
  char b[] = "Withdrawal";
  char c[] = "Cancel";
  char answer[40];

  printf("CHOOSE ONE:\n");
  printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
  fgets(answer, sizeof answer, stdin);
  printf("You select %s\n", answer);

  printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
  fgets(answer, sizeof answer, stdin);
  printf("You select %s\n", answer);

  printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
  fgets(answer, sizeof answer, stdin);
  printf("You select %s\n", answer);

  return 0;

Quote:
}

--
Simon.


Wed, 24 Nov 2004 10:08:54 GMT  
 a problem with strings

Quote:

> Hi everybody!

> I am a new newbie. This is a menu program, I want user to choose one
> character and display the choice .

The problem is you are overwriting your text messages with the user's
selection.  Also, scanf() is fairly evil and scanf("%s", buf) is very
dangerous, since scanf doesn't know how big "buf" is.

I must be bored...  See if you can digest this...

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define LINE_BUF 80

struct menu_item {
    const int item;
    const char *description;

Quote:
} menu[] = {

      {'a', "Deposit"},
      {'b', "Withdrawal"},
      {'c', "Cancel"},
      {'q', "Quit"},
      {0, NULL}

Quote:
};

void print_menu (FILE *ofp)
{
    struct menu_item *menu_ptr = menu;
    fprintf (ofp, "*********  MENU  *********\n");
    do  {
         fprintf (ofp, " %c - %s\n", (char) menu_ptr->item,
                                     menu_ptr->description);
         menu_ptr++;
    } while (menu_ptr->item != 0);
    fprintf (ofp, "**************************\n\n");
    fprintf (ofp, "choice> ");
    fflush (ofp);

Quote:
}

int get_choice (FILE *ifp)
{
    char line[LINE_BUF];
    int  choice = EOF;

    if (fgets(line, sizeof line, ifp) != NULL)
    {
        char *cptr = line;
        while (*cptr && isspace(*cptr))
           cptr++;
        choice = tolower (*cptr);
    }
    return choice;

Quote:
}

int print_choice (FILE *ofp, int choice)
{
    int status = 0;
    struct menu_item *menu_ptr = menu;
    do {
        if (choice == menu_ptr->item)
        {
            fprintf (ofp, "You selected: \"%s\"\n", menu_ptr->description);
            status = 1;
            break;
        }
        menu_ptr++;
    } while (menu_ptr->item != 0);
    return status;

Quote:
}

void print_bad_choice (FILE *ofp)
{
   fprintf (ofp, "ERROR: Invalid selection. Type 'q' to quit\n\n");

Quote:
}

int quit_program (int choice)
{
    return choice == 'q';

Quote:
}

int main (void)
{
    int choice = 0;

    do  {
         print_menu (stdout);
         choice = get_choice (stdin);
         if (choice != EOF)
         {
             if (!print_choice (stdout, choice))
                 print_bad_choice (stderr);
         }
    } while (choice != EOF && !quit_program (choice));

    return 0;

Quote:
}



Wed, 24 Nov 2004 10:19:06 GMT  
 a problem with strings
Here is my example, its almost like yours, but I changed it a little
I store strings in array, answer is written to char using getch()
function. And you should check ans for illegal value, so legal is
  0 <= ans-'a' < 3
(getch returns you ascii code, so when you press 'b' it returns 98.
Actually there is no difference between 98 and 'b' in C. You can write
'a'*'b'-'c'*'d')

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> /* getch() */

int main(void)

{
  char a[3][15] = {"Deposit", "Withdrawal", "Cancel"};
  char ans;

  printf("CHOOSE ONE:\n");
  printf(" a %s.\n b %s.\n c %s.\n", a[0], a[1], a[2]);
  ans=getch();
  printf("You select %s\n", a[ans-'a']);

  printf(" a %s.\n b %s.\n c %s.\n", a[0], a[1], a[2]);
  ans=getch();
  printf("You select %s\n", a[ans-'a']);

  printf(" a %s.\n b %s.\n c %s.\n", a[0], a[1], a[2]);
  ans=getch();
  printf("You select %s\n", a[ans-'a']);

  system("PAUSE");
  return 0;

Quote:
}



Wed, 24 Nov 2004 16:35:06 GMT  
 a problem with strings

train-u.com:

Quote:
> I am a new newbie. This is a menu program, I want user to choose one
> character and display the choice .
> {
>   char a[] = "Deposit";
>   char b[] = "Withdrawal";
>   char c[] = "Cancel";

>   printf("CHOOSE ONE:\n");
>   printf(" a %s.\n b %s.\n c %s.\n", a, b, c);
>   scanf("%s.\n%", &a);

I'm not sure about the trailing "%".

   scanf("%s.\n", &a);

You are using a string for both in and out purposes.

At the beginning, the 'a' string holds "Deposit".

Then you pass it's address to the scanf() function, and consequently, the
users characters are stored here, hence the "Deposit" string becomes "a".

Quote:
>   printf("You select %s\n", a, b, c);

This is a nonsense. You have three variables for one formatter.

   printf("You select %s\n", a);

Now, you display "a" instead of "Deposit"

Quote:
>   printf(" a %s.\n b %s.\n c %s.\n", a, b, c);

Same here.

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.



Wed, 24 Nov 2004 16:36:55 GMT  
 a problem with strings


Quote:
> Here is my example, its almost like yours, but I changed it a little
> I store strings in array, answer is written to char using getch()
> function. And you should check ans for illegal value, so legal is

getch() is not standard. It's only supported by some MS-DOS or Console
Win32 compilers.

Quote:
>   0 <= ans-'a' < 3
> (getch returns you ascii code, so when you press 'b' it returns 98.
> Actually there is no difference between 98 and 'b' in C. You can write
> 'a'*'b'-'c'*'d')

Please don't post non portable solutions in clc.

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.



Wed, 24 Nov 2004 16:59:19 GMT  
 a problem with strings

Quote:
> Here is my example, its almost like yours, but I changed it a little
> I store strings in array, answer is written to char using getch()

That is not a standard C function, and so is off topic in clc.

Quote:
> function. And you should check ans for illegal value, so legal is
>   0 <= ans-'a' < 3
> (getch returns you ascii code, so when you press 'b' it returns 98.
> Actually there is no difference between 98 and 'b' in C. You can write
> 'a'*'b'-'c'*'d')

Standard C does not require ASCII character codings. Moreover, there is no
requirement for letter codings to be sequential.

Quote:
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <conio.h> /* getch() */

This is a non standard header.

[snip]

Quote:
>   ans=getch();
>   printf("You select %s\n", a[ans-'a']);

>   system("PAUSE");

Non standard functions and non-portable constructs.

You might want to browse the FAQ before making further posts to clc.

  http://www.eskimo.com/~scs/C-faq/top.html

--
Peter



Wed, 24 Nov 2004 21:17:11 GMT  
 a problem with strings

Quote:


>> Here is my example, its almost like yours, but I changed it a little
>> I store strings in array, answer is written to char using getch()
>> function. And you should check ans for illegal value, so legal is

>getch() is not standard. It's only supported by some MS-DOS or Console
>Win32 compilers.         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 ^^^^^^^^^^^^^^^
Huh?!?  When did they drop getch() from the curses library?

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Fri, 26 Nov 2004 19:45:16 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Intermittent problem creating strings using MC++

2. New problem with string and compilers

3. having a problem w/ string.insert

4. Problem with strings and pointers

5. Problem with strings and pointers

6. problem with string class

7. Problem reading strings from stdin

8. Problem with strings

9. New problem with string and compiler

10. Problems Regarding Strings in VC++

11. Problem = adjusting strings/integers in a row/column

12. problem with string parsing

 

 
Powered by phpBB® Forum Software