User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,904 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,867 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1647 | Replies: 8
Reply
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Help array problems

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,164
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: array problems

  #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  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

  #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  
Join Date: Aug 2005
Posts: 596
Reputation: SpS is on a distinguished road 
Rep Power: 5
Solved Threads: 31
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: array problems

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,164
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: array problems

  #5  
Sep 1st, 2005
did you try this
char buffer[6];
for(int i = 0; i < 5; i++)
   cin >> buffer[i];
buffer[5] = 0;

cout << buffer << endl;
Reply With Quote  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

  #6  
Sep 1st, 2005
Originally Posted by Ancient Dragon
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
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,164
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: array problems

  #7  
Sep 1st, 2005
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]);
}
Reply With Quote  
Join Date: Aug 2005
Posts: 596
Reputation: SpS is on a distinguished road 
Rep Power: 5
Solved Threads: 31
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

News Re: array problems

  #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  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

  #9  
Sep 1st, 2005
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC