0

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.

8
  • "..the output should be hlodworlwl. Now it is showing like this hlodworlwl", this two output are the same. am i missing something? Commented Apr 30, 2016 at 19:12
  • I don't really understand what should your program do, but I see delcaration of you array like this: char array[3][3]; , so it can hold 9 characters, However string hel low orl d has 10 characters. Commented Apr 30, 2016 at 19:12
  • im so soory guys, i am sleep deprive thus i made a lot of mistake and lack explaination. the second "hlodworlwl" should be hloworlwl. @bkVnet i tried using before the array[5][5] and end up getting garbage value for those empty. How do i solve that? Commented Apr 30, 2016 at 19:30
  • try initializing the array like char array[5][5]={0} Commented Apr 30, 2016 at 19:36
  • @bkVnet it is placed in namespace scope, so it will be initialized with zeroes. Commented Apr 30, 2016 at 19:39

1 Answer 1

1

The answer is simple "helloworld" has 10 characters. Your array stores only 9. D is never in the array to be output.

Sign up to request clarification or add additional context in comments.

2 Comments

hello please ignore the previous message, im so soory, i am sleep deprive thus i made a lot of mistake and lack explaination. i tried using before the array[5][5] and end up getting garbage value for those empty field . How do i solve that?Thank you for your help.i also have another question, how can i make the cin to only logical value. i have put the question on top. hope you can help me out
@Bhappy you can delete comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.