Could use a little help, Strings, arrays, and functions.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Could use a little help, Strings, arrays, and functions.

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    2

    Could use a little help, Strings, arrays, and functions.

    Alrighty, so I'm working on a piece of code for my CS class. I'm fairly new to this, and I'm making some mistake in trying to get this assignment together. Here is the code I'm using...

    #include <iostream>
    #include <cstring> // I am using the C++ strings library, not character arrays.

    using namespace std;

    void getName(string);
    void giveName(string);

    string namesArray[10];
    int main(){
    int getOption = 0;
    int loopValue = 0;
    int loopValue2 = 0;
    do{
    cout << "Please enter a 1 if you want to enter names, a 2 if you wish to find a name, or a 0 if you want to exit." << endl;
    do{
    cin >> getOption;
    if (getOption == 0){
    cout << "Goodbye." << endl;
    return 0;
    }
    if (getOption < 0 || getOption > 2){
    cout << "Please enter a valid selection." << endl;
    loopValue2 = 0;
    }
    else{
    if (getOption == 1){
    getName(namesArray[10]);
    loopValue2 = 1;
    }
    else{
    giveName(namesArray[10]);
    loopValue2 = 1;
    }
    }
    }while(loopValue2 = 0);
    } while(loopValue = 0);
    }

    void getName(string namesArray[]){
    string firstName, lastName;
    for(int i = 0; i < 10; i++){
    cout << "When you are done entering names, please type exit for the next person's first name." << endl;
    cout << "Please enter person number " << i++ << "'s first name." << endl;
    cin >> firstName;
    if (firstName == "exit"){
    i = 10;
    break;
    }
    cout << "Please enter person number " << i++ << "'s last name." << endl;
    cin >> lastName;
    lastName = " " + lastName;
    namesArray[i] = firstName + lastName; // Concat to give the full name in 1 part of the array.
    }
    }
    Albeit there are probably thousands of better ways to write this, I'm still new and learning. Now, my problem is in line 35(at the moment) in bold. I'm getting an error I don't know how to make sense of. It says..

    "Undefined first referenced
    symbol in file
    giveName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)/var/tmp//ccyPQIEC.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status"

    I've tried passing the string array using nothing after namesArray, and it tells me that I can't do that by saying..

    "error: conversion from `std::string*' to non-scalar type `std::string' requested"

    Also, if I use namesArray[], I says...

    "error: expected primary-expression before ']' token".

    I am at a loss. Can anyone tell me where I am making my mistake(s)?

  2. #2
    Join Date
    Apr 2008
    Posts
    2
    Update:

    Started using a 2d array and it's working now. Thanks for look if you did.

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    If your array has 10 elements, they are numbered 0 to 9. There is no element [10].

    void getName(string namesArray[])
    {}
    you are writing the definition as if you are expecting to pass in an array of strings

    void getName( string [] nameArray )
    {}

    however, when you are calling the getName function in the boldened statement, you are passing only one string of the string array.

    So ... what is it going to be? Look in your textbook or notes about using an array as an argument for a function, and if you truly are passing an array all you use to refer to the array is the array's name, without any brackets, since this refers to the start of the contiguous memory locations where the elements of the array are stored.

  4. #4
    Join Date
    May 2007
    Posts
    843
    Here is the shortcut for you :

    void getName(string []); // Function Declaration;
    getName(nameArray); // Function caller

    void GetName(string nameArray[Size]){}

    I hope this help/

Similar Threads

  1. Passing Arrays into Functions
    By Catalyst8487 in forum C++
    Replies: 7
    Last Post: 05-08-2007, 08:48 AM
  2. arrays and functions
    By ChrisB in forum C++
    Replies: 3
    Last Post: 03-13-2007, 03:58 PM
  3. Allocating memory in VB (Custom strings)
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 4
    Last Post: 03-04-2002, 08:04 AM
  4. Arrays and Functions
    By Matt in forum Java
    Replies: 1
    Last Post: 09-24-2000, 02:05 AM
  5. Strings and memory management
    By Brian Leung in forum VB Classic
    Replies: 36
    Last Post: 09-18-2000, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center