I am working on a program that uses an array called Game that can store 26 characters. It asks the user to to enter a single letter and a number between 1 and 26. It then places the letter they entered into the position in the array referenced by the number they entered. I used a while loop to see if they wanted to play again. It then displayed the contents of the array with a for loop.

The problem that i am having is that when the program executes it enters the letters in the correct spot but it also puts some different characters in other spots that i do not want. i only want the letters in the assigned spot. Can somone look at my code and let me know where my code is incorrect and give me some guidence.

Thank you so much

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    
    //Declarations
    char Game[26];
    char letter;
    char Repeat = 'Y'; 
    int number;

    while (Repeat == 'Y'  || Repeat == 'y'){
          
    //Input
    
    cout << "Please enter a letter: ";
    cin >> letter;
    cout << "Please enter a number between 1 and 26: ";
    cin >> number;
    
        while (number <= 0 || number >= 27){
        cout << "Invalid Entry." << endl;
        cout << "Please enter a number between 1 and 26: ";
        cin >> number;
        }
        
    Game [number] = letter;
        
    cout << "Do you want to play again?  (Y or N):  ";
    cin >> Repeat;
    cout << endl;    
    }
    cout << "Game Over!" << endl << endl;
    
    //Process
    cout << "The array contains:" << endl;
    
    for (int x=1; x<=26; x++)
        cout << "Element at index " << x << " is " << Game[x] << endl;
    
    //Output
    
    
    system ("pause");
    return 0;
}

Several problem.

Either Increase your array size

char Game[ 26 + 1 ];

Or use a (-1) index on everything.
You're accepting a number from 1...26 but you're stuffing it in the array at 1...26. The array is 0...25 as it is 26 in size!
So either
Game[ number - 1 ] = letter;
or as you are
Game[ number ] = letter;
as you increased the array to accomodate that 26th letter!

Your other problem is garbage in, garbage out.
Pre-initialize

for (n = 0; n < 26; n++ )
       Game[n] = 0;

And you have that same [N] problem on the printing

//for (int x=1; x<=26; x++)  // use me if you increase array +1
// cout << "Element at index " << x << " is " << Game[x] << endl;
   // OR
for (int x=0; x < 26; x++)
        cout << "Element at index " << x << " is " << Game[x] << endl;
  // But I wouldn't recommend
//for (int x=1; x <= 26; x++)
//        cout << "Element at index " << x << " is " << Game[x-1] << endl;

When you create a new array, the elements in the array are not initialized to any default value. Therefore whatever happens to be in the memory where the array is created, it will be that until you assign a new value to it. So if you want it to default to a space character or something, you need to iterate through the array and set each value to that character.

Ah good catch. In my posing I initialized to zero, but should have been (' ') space character so its printable when you print!

In my for loops I don't want 0 to be displayed so I started with 1 (not sure if its okay to do that though), then when I run the code it only displays the last letter you entered on the last number. I want it to display all letters entered on all the coresponding numbers entered.

Did I just dig myself into a deeper mess?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    
    //Declarations
    char Game[26];
    char letter;
    char Repeat = 'Y'; 
    int number;
    
    char n;
    
    while (Repeat == 'Y'  || Repeat == 'y'){
          
    //Input
    
    cout << "Please enter a letter: ";
    cin >> letter;
    cout << "Please enter a number between 1 and 26: ";
    cin >> number;
    
        while (number <= 0 || number >= 27){
        cout << "Invalid Entry." << endl;
        cout << "Please enter a number between 1 and 26: ";
        cin >> number;
        }
        
    for (n = 1; n < 27; n++ )   
       Game[n] = ' ';
       Game [number] = letter;
        
    cout << "Do you want to play again?  (Y or N):  ";
    cin >> Repeat;
    cout << endl;    
    }
    cout << "Game Over!" << endl << endl;
    
    //Process
    cout << "The array contains:" << endl;
    
    for (int x=1; x<27; x++)
        cout << "Element at index " << x << " is " << Game[x] << endl;
    
    //Output
    
    
    system ("pause");
    return 0;
}

Yes you did!

I made corrections and commented out the inpropr code I noticed!

int main()
{
    
    //Declarations
    char Game[26];
    char letter;
    char Repeat = 'Y'; 
    int number;
    
    char n;

// Moved initialization outside the loop, to top of function!    
    for (n = 0; n < 26; n++ )   
       Game[n] = ' ';

    while (Repeat == 'Y'  || Repeat == 'y'){
          
    //Input
    
    cout << "Please enter a letter: ";
    cin >> letter;
    cout << "Please enter a number between 1 and 26: ";
    cin >> number;
    
        while (number <= 0 || number >= 27)
        {
            cout << "Invalid Entry." << endl;
            cout << "Please enter a number between 1 and 26: ";
           cin >> number;
        }
// Moved to top of file!!!        
//    for (n = 1; n < 27; n++ )   
//       Game[n] = ' ';
// You didn't pay attention to your (-1) issue!
//       Game [number] = letter;
       Game [ number -1 ] = letter;
        
    cout << "Do you want to play again?  (Y or N):  ";
    cin >> Repeat;
    cout << endl;    
    }
    cout << "Game Over!" << endl << endl;
    
    //Process
    cout << "The array contains:" << endl;
    
//    for (int x=1; x<27; x++)
    for (int x=0; x<26; x++)
        cout << "Element at index " << x << " is " << Game[x] << endl;

        
    system ("pause");
    return 0;
}

Since you always enter the first time, you could change your while loop to a do-while loop! Test for the play again at the bottom of the loop.

You might want some visual feedback, print the array after each letter entry for positive feedback.
But print on a single line. No # references! But increase your array size to Game[27], and in initialization set the Game[26] = 0;
set the terminator so you could just print the stiing Game at once!
cout << Game << endl;

Shorten the question "Do you want to play again?" To "Play Again?" Less verbose and still clearly understood!

And definitely move "Game Over' AFTER the game is really over.
Technically data entry is over, but not the game as you haven't finished printing information!

Also n is an index. Change it from char to int.

This article has been dead for over six months. Start a new discussion instead.