943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2123
  • C++ RSS
Sep 1st, 2005
0

array problems

Expand Post »
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?
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 1st, 2005
0

Re: array problems

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?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 1st, 2005
0

Re: array problems

Quote originally posted by Ancient Dragon ...
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?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 1st, 2005
0

Re: array problems

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;
}
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 1st, 2005
0

Re: array problems

did you try this
C++ Syntax (Toggle Plain Text)
  1. char buffer[6];
  2. for(int i = 0; i < 5; i++)
  3. cin >> buffer[i];
  4. buffer[5] = 0;
  5.  
  6. cout << buffer << endl;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 1st, 2005
0

Re: array problems

Quote originally posted by Ancient Dragon ...
did you try this
C++ Syntax (Toggle Plain Text)
  1. char buffer[6];
  2. for(int i = 0; i < 5; i++)
  3. cin >> buffer[i];
  4. buffer[5] = 0;
  5.  
  6. 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
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 1st, 2005
0

Re: array problems

By "user enters A" what exactly do you mean? one character? a whole string of characters? a number?
C++ Syntax (Toggle Plain Text)
  1. std::string buffer[5];
  2. for(int i = 0; i < 5; i++)
  3. {
  4. cout << "Enter string #" << i+1;
  5. getline(cin,buffer[i]);
  6. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 1st, 2005
0

Re: array problems

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;
}
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 1st, 2005
0

Re: array problems

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

C++ Syntax (Toggle Plain Text)
  1. char buffer[6];
  2. for(int i=0;i<6;i++)
  3. {
  4. cin>>buffer[i];
  5. cout << "\n";
  6. for(int j=0;j<i;j++)
  7. cout<<buffer[j];
  8. }

why does it not register the first char input?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I Need A Willing C++ Programmer
Next Thread in C++ Forum Timeline: C++:three-digit number all odd, eve, or both





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC