Hello boys and girls,

I'm wondering around with arrays and I'm trying to get one up so that it grabs a user inputt'd char and adds it on then prints out the contents of the array so far

ie "Enter character" :
User enters : 'A'

Prints out A on screen

"Enter character" :
User enters B

Printsout A and B on screen.

I've got no problem populating and printing out arrays, but I cant figure this one out.

I've thought adding to strings but no luck there.

I've got this so far but it doesnt help at all and I know why but not sure how to defeat this...

...
int i;
for (i = 0 ; 1 < 5 ; i++)
{
cin >> buffer[i];
}

for (i = 0 ; i < 5; i++)
{
cout << buffer[i]
}

whilst this code doesnt do what ask'd 
I thought'd about how to get one step closer:

for (i = 0 ; 1 < 5 ; i++)
{
   cin >> buffer[i];
  {
      for (i = 0 ; i < 5; i++)
     {
     cout << buffer[i]
     }
  }
}

but still fails....can anyone help us out here?

Edited 3 Years Ago by diafol: fixed formatting

what do you mean "it failes"? it won't compile? it won't run? what error(s) do you get? Note: unless its just a posting error, it needs a semicolor at the end of the cout line. Also, how is buffer declared?

what do you mean "it failes"? it won't compile? it won't run? what error(s) do you get? Note: unless its just a posting error, it needs a semicolor at the end of the cout line. Also, how is buffer declared?

The code compiles, butr I'm trying to reach a goal as stated above, could anyone hlep me out with it?

i tried this and it worked fine

#include<iostream.h>
int main()
{
char buffer[5][10];


for (int i = 0 ; i < 5 ; i++)
        {
            cin >> buffer[i];
        }       
    for (i = 0 ; i < 5; i++)
        {
            cout << buffer[i];
        }
return 0;
}

Edited 3 Years Ago by pyTony: fixed formating

did you try this

char buffer[6];
for(int i = 0; i < 5; i++)
   cin >> buffer[i];
buffer[5] = 0;

cout << buffer << endl;

Sorry to put a downer on things but I've built these, the next objective was to create something like this:

user enters A
konsole windows prints A

user enters B
konsolewindow prints AB

By "user enters A" what exactly do you mean? one character? a whole string of characters? a number?

std::string buffer[5];
for(int i = 0; i < 5; i++)
{
  cout << "Enter string #" << i+1;
  getline(cin,buffer[i]);
}

i guess this will solve your problem

#include<iostream>
using namespace std;
int main()
{
    char buffer[6];
    for(int i=0;i<6;i++)
    {
        cin>>buffer[i];
    for(int j=0;j<=i;j++)
        cout<<buffer[j];
    }
    return 0;
}

Edited 3 Years Ago by pyTony: fixed formating

yes thats the one!
man a simple code seem'd so hard to do, thanks back to my game now....

char buffer[6];
for(int i=0;i<6;i++)
	{
	cin>>buffer[i];
	cout << "\n";
		for(int j=0;j<i;j++)
		cout<<buffer[j];
	}

why does it not register the first char input?

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