array problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

array problems

 
0
  #1
Sep 1st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: array problems

 
0
  #2
Sep 1st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

 
0
  #3
Sep 1st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: array problems

 
0
  #4
Sep 1st, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: array problems

 
0
  #5
Sep 1st, 2005
did you try this
  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;
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

 
0
  #6
Sep 1st, 2005
Originally Posted by Ancient Dragon
did you try this
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: array problems

 
0
  #7
Sep 1st, 2005
By "user enters A" what exactly do you mean? one character? a whole string of characters? a number?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: array problems

 
0
  #8
Sep 1st, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

 
0
  #9
Sep 1st, 2005
yes thats the one!
man a simple code seem'd so hard to do, thanks back to my game now....

  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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC