0

So for my program, I have to allow the user to enter a character then ask them how many of that character they want. I'm saving the character and the quantity into two different arrays. I've attached my code but when I go to display the array, it doesn't print the character that I have put in.

#include <stdio.h>
#include <stdlib.h>
#define pause system ("pause")

main(){
    char characterOfChoice = '0', charChoice = 'c', charArray[100];
    int quantityOfChar = 0, i = 0, intChoice = 0, intArray[100];

    printf("Enter a character of your choice  ");
    scanf("%c", &charArray[charChoice]); //save char to a temp char
    printf("Enter a quantity of that character  ");
    scanf("%d", &intArray[intChoice]); //save int to a temp value

    for(i=0; i<100; i++){
        charArray[charChoice] = '!';
        intArray[i] = 0;
    }//End for Loop

    for(i=0; i<10; i++){
        printf("%c ", charArray[charChoice]);
            }

    pause;
}//End main
5
Contributors
4
Replies
14
Views
4 Years
Discussion Span
Last Post by faroukmuhammad
0

Sevaral problems here:

1) You're using charChoice as an index for accessing your charArray[] array. The character 'c' is equal to 99, so you're always just overwriting the last element of your array.

2) You successfully save the character that the user types into charArray[charChoice], but then your for loops overwrites that value a hundred times with an exclamation mark character, and it also sets everything in intArray[] to 0. This is why it's not remembering what you've wrote.

3) You declare the variables characterOfChoice and quanitityOfChar, but you're never using them anywhere.

I think it would be in your best interest to comment your code, read what you've wrote and try to understand what your program is doing.

0

To save the entered string of characters, write this:scanf("%s", charArray);
If you need a single character, better use charArray[array_item_number]=getch(); or use scanf("%c", &charArray[array_item_number]);(or scanf("%c", charArray + array_item_number);).
If I understand correctly, what you want, you could write:

#include <stdio.h>

void main()
{
    char character[10];
    int quantity;
    printf("\nEnter a character of your choice  ");
    scanf("%c", character + 3);//or scanf("%c", &character[3]);
    printf("\nEnter a quantity of that character  ");
    scanf("%d", &quantity);
    printf("\n");
    for(int i = 0; i < quantity; i++)
    {
        printf("%c ", character[3]);//or printf("%c ", *(character + 3);
    }
    printf("\n\n");
}

For this program does not need arrays. I make an array for you to see how to use them.
Sry for Google English:)

Edited by john.edream

0

Hi I am new to arraylists and java and I was wondering if someone could help me or give me pointers on how to create a program that allows the user to repeatedly enter directory entries from the keyboard and store them in an arraylist.

enter name:
enter telephone number:

and then ask if the user wants to enter another one

enter another: Y/N

0

This is the modification;

#include <stdio.h>
#include <stdlib.h>
#define pause system ("pause") 
main(){
  char characterOfChoice ='0', charChoice = 'c', charArray[100];
  int quantityOfChar = 0, i = 0 intChoice = 0, intArray[100];
  printf("Enter a character of your choice ");
  scanf("%c", &charArray[charChoice]); //save char to a temp char
  printf("Enter a quantity of that character ");
  scanf("%d", &intArray[intChoice]); //save int to a temp value 
     for(i=0; i<intArray[intChoice]; i++){
       printf("%c ", charArray[charChoice]);
      } 
   pause;
}//End main

Note:
*line 14-17 is not needed
*the for loop in line 19 is suppose to compare with quantity which is in intArray[charChoice]

And John is right you don't need an array. Array comes to play if you have many values of the same family, like; scores of student in a class, salary of employees, characters in a sentence, etc.

Edited by faroukmuhammad

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.