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

Recommended Answers

All 4 Replies

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.

Member Avatar for john.edream

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.