954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

array problems

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...

[code]

...
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?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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?

Ancient Dragon
Retired & Loving It
Team Colleague
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 
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?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

i tried this and it worked fine

#include
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;
}

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

did you try this

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

cout << buffer << endl;
Ancient Dragon
Retired & Loving It
Team Colleague
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

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

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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]);
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

i guess this will solve your problem

#include
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<

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You