Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   I'm new at programming and i need help (http://www.programmingforums.org/showthread.php?t=14869)

thenewb Jan 4th, 2008 9:48 PM

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!!!:)

Sane Jan 5th, 2008 12:13 AM

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. ;)

titaniumdecoy Jan 5th, 2008 12:28 AM

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*.

Jabo Jan 5th, 2008 1:11 AM

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:
:

  1. cin>>"Name: ">>variablename;


Am I wrong?

Ancient Dragon Jan 5th, 2008 8:08 AM

Re: I'm new at programming and i need help
 
Quote:

Originally Posted by Jabo (Post 139162)
It's been a while since I learned C++, but it seems you could have something like:
:

  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.

thenewb Jan 5th, 2008 12:12 PM

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 Jan 5th, 2008 12:20 PM

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!!!:)

Jessehk Jan 5th, 2008 1:08 PM

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:
:

  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.
:

  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. }


mbd Jan 5th, 2008 2:26 PM

Re: I'm new at programming and i need help
 
Quote:

Originally Posted by Ancient Dragon (Post 139175)
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.

Jimbo Jan 5th, 2008 5:02 PM

Re: I'm new at programming and i need help
 
This line looks suspect: string g_pnArray1 = new char[g_nEmploy];


All times are GMT -5. The time now is 3:39 AM.

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