Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 4th, 2008, 8:48 PM   #1
thenewb
Newbie
 
thenewb's Avatar
 
Join Date: Jan 2008
Posts: 3
Rep Power: 0 thenewb is on a distinguished road
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;
}
The problem is that I can't make the program say "Name?" and stop to let the user type in the name. All it does is print "Name?" as many times as you said how many employees you have. Also is there any way to change the array element number each time you type in a name?
Thank You for your replies!!!
thenewb is offline   Reply With Quote
Old Jan 4th, 2008, 11:13 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Jan 4th, 2008, 11:28 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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];
}
You should also use std::string rather than char*.

Last edited by titaniumdecoy; Jan 4th, 2008 at 11:51 PM.
titaniumdecoy is offline   Reply With Quote
Old Jan 5th, 2008, 12:11 AM   #4
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
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)
  1. cin>>"Name: ">>variablename;

Am I wrong?
Jabo is offline   Reply With Quote
Old Jan 5th, 2008, 7:08 AM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 544
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: I'm new at programming and i need help

Quote:
Originally Posted by Jabo View Post
It's been a while since I learned C++, but it seems you could have something like:
C++ Syntax (Toggle Plain Text)
  1. cin>>"Name: ">>variablename;

Am I wrong?
Yes, you are wrong. cin is for keyboard input only and a literal string such as "Name:" can not be the target buffer. If you want to display that string then use cout as shown by titaniumdecoy's post.
Ancient Dragon is offline   Reply With Quote
Old Jan 5th, 2008, 11:12 AM   #6
thenewb
Newbie
 
thenewb's Avatar
 
Join Date: Jan 2008
Posts: 3
Rep Power: 0 thenewb is on a distinguished road
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;
}
and ran it and say I type in 5 for the number of employees. It shows "name?" and I enter the name and then 4 more "name?"'s appear before I can enter again. Also when I print out the array, I found out that it stores each letter in each element so if i type in jeff for the first and jay for the next, I end up with jeffj and a jumble of characters. Does anybody know how to store strings in each array element instead of a letter in each array element?
Once again Thank You for your replies!!!!
thenewb is offline   Reply With Quote
Old Jan 5th, 2008, 11:20 AM   #7
thenewb
Newbie
 
thenewb's Avatar
 
Join Date: Jan 2008
Posts: 3
Rep Power: 0 thenewb is on a distinguished road
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!!!
thenewb is offline   Reply With Quote
Old Jan 5th, 2008, 12:08 PM   #8
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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)
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.  
  5. // An alias for a list of strings.
  6. // These are STL containers and they are
  7. // preferred to using arrays.
  8. typedef std::list<std::string> EmployeeList;
  9.  
  10. // Print out the employee's names
  11. // This function could be defined more simply, but this
  12. // is in the STL style of using iterators.
  13. void printEmployees( EmployeeList::const_iterator begin, EmployeeList::const_iterator end ) {
  14. std::cout << "Employees:\n----------" << std::endl;
  15. for (; begin != end; begin++ )
  16. std::cout << "Name: " << *begin << std::endl;
  17. }
  18.  
  19. // This function takes an input stream (like std::cin),
  20. // and causes it to ignore all input before and including
  21. // a newline character.
  22. inline void ignoreNewline( std::istream &is ) {
  23. is.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  24. }
  25.  
  26. int main() {
  27. int numbEmploy;
  28.  
  29. // We're doing 3 things in this while loop
  30. // 1) Printing the prompt
  31. // 2) Reading the input and checking that there are no errors
  32. // 3) Making sure the input (if it's valid) is above 0
  33. while ( (std::cout << "How many employees?: ") &&
  34. !(std::cin >> numbEmploy) ||
  35. (numbEmploy < 0) ) {
  36. // If any of these conditions, fail...
  37. // Clear the invalid stream.
  38. // We have to do this because when they entered the input,
  39. // the newline character, '\n' is not read so it's left over
  40. // the next time we try to get input and the program looks
  41. // like it skipped something.
  42. // The call to std::cin.clear() clears the error bits
  43. // and allows more input to be read.
  44. std::cout << "That's not a valid integer..." << std::endl;
  45. std::cin.clear();
  46. ignoreNewline( std::cin );
  47. }
  48.  
  49. // See above comment block for why this is needed.
  50. ignoreNewline( std::cin );
  51.  
  52. EmployeeList employees;
  53. std::string name;
  54. for ( int i = 0; i < numbEmploy; i++ ) {
  55. std::cout << "Name?: ";
  56.  
  57. // Read a line of input
  58. std::getline( std::cin, name );
  59.  
  60. // Append the name to the list
  61. employees.push_back( name );
  62. }
  63.  
  64. std::cout << std::endl;
  65. printEmployees( employees.begin(), employees.end() );
  66. }

I can sympathize with any pain that code may cause.

As you your second question: sure.
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // A simple struct
  5. struct Person {
  6. std::string name;
  7. int age;
  8. };
  9.  
  10. const int NUMB_PEOPLE = 3;
  11.  
  12. int main() {
  13. // An array of People
  14. Person *people = new Person[NUMB_PEOPLE];
  15.  
  16. // Do stuff with people...
  17.  
  18. delete [] people;
  19. }
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; Jan 5th, 2008 at 12:36 PM.
Jessehk is offline   Reply With Quote
Old Jan 5th, 2008, 1:26 PM   #9
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: I'm new at programming and i need help

Quote:
Originally Posted by Ancient Dragon View Post
cin is for keyboard input only
it would be more accurate to say cin is for standard input only. c++ does not require a keyboard at all. cin could point to any input device and is commonly redirected from a file.
mbd is offline   Reply With Quote
Old Jan 5th, 2008, 4:02 PM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC