Reputation: 13
i have a array program which will read out the colunm based on user input. Somehow it is giving me an error if there are empty field in the array. For example if i read in the word helloworl my program will work. However if i read in helloword, my program will not print the last character which is d. I have spend days trying to find out how to solve, i hope someone can help me out please. Thank you.
the output should be hlodworlwl. Now it is showing like this hloworlwl missing the last row.
One more thing, my program will take the user input and sort and print out the array. How can i error proof the program to make sure user type (logically) like 123 321 213 etc and not 111 222 134?
if i read in helloworld and the user enter 123, my 2d array should be
hel
low
orl
d
the output should be hlodworlwl. Now it is showing like this hloworlwl missing the last row.
One more thing, my program will take the user input and sort and print out the array. How can i error proof the program to make sure user type (logically) like 123 321 213 etc and not 111 222 134?
hel low orl
#include <iostream>
#include <string>
using namespace std;
char array[5][5];
//function that prints the column c of the array
void printArray(int c)
{
for(int i=0; i<3; i++)
{
cout << array[i][c];
}
}
int main() {
string alphabate;
string a="helloworld";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
array[i][j] = a[j+ (i * 3)];
}
}
cout << "Enter some alphabate:";
cin >> alphabate;
//checking the input parameters
for (int j=0; j<3; j++) {
if (alphabate[j] == '1' || alphabate[j] == 'a')
{
printArray(0);
}
else if (alphabate[j] == '2' || alphabate[j] == 'b')
{
printArray(1);
}
else if (alphabate[j] == '3' || alphabate[j] == 'c')
{
printArray(2);
}
}
return 0;
}
Once again, thank you for reading through my problem.
Upvotes: 0
Views: 204
Reputation: 166
The answer is simple "helloworld" has 10 characters. Your array stores only 9. D is never in the array to be output.
Upvotes: 1