![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2008
Posts: 3
Rep Power: 0
![]() |
I'm new at programming and i need help
Hi
I'm new at programming and I kind of need help. I trying to create a programming that accept names and in those names you can enter data in them. Well I started creating it but ran in to a few problems. Here's the code: #include "stdafx.h"
#include <iostream>
int main()
{
using namespace std;
cout<<"How many employee's do you have?"<<endl;
int g_nEmploy;
cin>>g_nEmploy;
char *g_pnArray1 = new char[g_nEmploy];
for (int iii=0; iii<g_nEmploy; iii++)
cout<<"Name?"<<endl;
cin>>g_pnArray1[0];
return 0;
}Thank You for your replies!!! ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Re: I'm new at programming and i need help
for (int iii=0; iii<g_nEmploy; iii++)
cout<<"Name?"<<endl;
cin>>g_pnArray1[0];Is equivilent to: for (int iii=0; iii<g_nEmploy; iii++) {
cout<<"Name?"<<endl;
}
cin>>g_pnArray1[0];But neither of the above are equivilent to: for (int iii=0; iii<g_nEmploy; iii++) {
cout<<"Name?"<<endl;
cin>>g_pnArray1[0];
}The way you indent the lines is irrelevant. Only the braces matter. If braces are excluded where a new block is initiated, only the first line/block is included in the new block. The latter format is the one you want, because both the output and input statements belong to the loop. Otherwise, with the first example, only the output statement belongs to the loop block, thus the input statement does not execute after each output. Hope that helps. Edit: Didn't see your second question. If you want to change the array element each time a name is entered, realize what the index is, and then look at the value of iii for each iteration. I'll let you figure this one out, see if you can make any connections.Post Edit: Ah, no worries. titanium beat me to it. My suggestion for you to try it yourself was more laziness on my part than anything. ![]() Last edited by Sane; Jan 4th, 2008 at 11:30 PM. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Re: I'm new at programming and i need help
You probably want to update the array index you are writing to as well:
for (int iii=0; iii<g_nEmploy; iii++) {
cout<<"Name?"<<endl;
cin>>g_pnArray1[iii];
}Last edited by titaniumdecoy; Jan 4th, 2008 at 11:51 PM. |
|
|
|
|
|
#4 |
|
Not a user?
Join Date: Sep 2007
Posts: 272
Rep Power: 2
![]() |
Re: I'm new at programming and i need help
It's been a while since I learned C++, but it seems you could have something like:
C++ Syntax (Toggle Plain Text)
Am I wrong? |
|
|
|
|
|
#5 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 544
Rep Power: 4
![]() |
Re: I'm new at programming and i need help
Quote:
|
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2008
Posts: 3
Rep Power: 0
![]() |
Re: I'm new at programming and i need help
Hi,
Thank you for your replies!! ![]() Though when I edited the code: #include "stdafx.h"
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout<<"How many employee's do you have?"<<endl;
int g_nEmploy;
cin>>g_nEmploy;
string g_pnArray1 = new char[g_nEmploy];
for (int iii=0; iii<g_nEmploy; iii++)
{ cout<<"Name?"<<endl;
cin>>g_pnArray1[iii];
}
cout<<g_pnArray1<<endl;
system("pause");
return 0;
}Once again Thank You for your replies!!!! ![]() ![]() ![]() |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jan 2008
Posts: 3
Rep Power: 0
![]() |
Re: I'm new at programming and i need help
Edit:I was also wondering if its possible to put a struct in each element of an array. Thank You!!!
![]() |
|
|
|
|
|
#8 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Re: I'm new at programming and i need help
I'd be happy to be corrected, but I'd say that this is "correct" C++ for your previous problem:
c++ Syntax (Toggle Plain Text)
I can sympathize with any pain that code may cause. ![]() As you your second question: sure. c++ Syntax (Toggle Plain Text)
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Jan 5th, 2008 at 12:36 PM. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: I'm new at programming and i need help
|
|
|
|
|
|
#10 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Re: I'm new at programming and i need help
This line looks suspect:
string g_pnArray1 = new char[g_nEmploy];
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|