Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,192 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,371 people online right now. Registration is fast and FREE... Join Now!




Error: subcript requires array or pointer type

 
Reply to this topicStart new topic

Error: subcript requires array or pointer type, Help with error message?

Paige1985
29 May, 2008 - 10:02 PM
Post #1

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 3

I'm fairly new to C++ and I keep getting an error message when I compile this segment of code. I'm trying to sort first, middle and last name. I'm not sure if my logic works yet because it won't compile.



CODE

#include <iostream>
#include <string>                
#include "Name.h"
using namespace std;


void DisplayName(char*Name)
{
    int *maxName = new int[400];
    int name = maxName[400];


if (name == 0)
{
    cout << "Error: memory could not be allocated" << endl;
};
for (int i=0; i<name; i++)            
    {
        name[i] = '\0';     [color=#CC0000]// ERROR: subscript requires array or pointer type[/color]  
    }
{
    cout << "Your name is: " << name << endl;
        
}
}

User is offlineProfile CardPM
+Quote Post

KYA
RE: Error: Subcript Requires Array Or Pointer Type
29 May, 2008 - 10:21 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,049



Thanked: 124 times
Dream Kudos: 1200
My Contributions
There's no main. What is included in "Name.h"?
User is online!Profile CardPM
+Quote Post

Paige1985
RE: Error: Subcript Requires Array Or Pointer Type
31 May, 2008 - 07:09 AM
Post #3

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 3

QUOTE(KYA @ 29 May, 2008 - 11:21 PM) *

There's no main. What is included in "Name.h"?


Sorry about that. I had to split the program into three files (header, implementation, main)
Here are all three files.

CODE

void DisplayName(char*Name);


CODE

#include <iostream>
#include <string>
#include "Name.h"
using namespace std;


void DisplayName(char*Name)
{
    int *maxName = new int[400];
    int name = maxName[400];


if (name == 0)
{
    cout << "Error: memory could not be allocated" << endl;
};
for (int i=0; i<name; i++)
    {
        name[i] = '\0';  
    }
{
    cout << "Your name is: " << name << endl;
        
}
}


CODE

#include <iostream>
#include <string>
#include "Name.h"
using namespace std;
int main ()
{
const int NAME = 400;
char nameArray[NAME];

cout << "Please enter your last name: ";
cin >> nameArray;

cout << "Please enter your first name: ";
cin >> nameArray;

cout << "Please enter your middle name: ";
cin >> nameArray;

DisplayName(nameArray);

system("PAUSE");

}












User is offlineProfile CardPM
+Quote Post

mikeblas
RE: Error: Subcript Requires Array Or Pointer Type
1 Jun, 2008 - 08:18 AM
Post #4

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions
The error message means what it says. When you use a subscript on a variable, that variable has to be a pointer or an array. You're using a subscript on the variable name in your DisplayName() function, but name is not a pointer--it's a scalar integer.

I can't quite figure out what it is you're trying to do in the DisplayName() function. What's the goal of your project?


User is offlineProfile CardPM
+Quote Post

Paige1985
RE: Error: Subcript Requires Array Or Pointer Type
1 Jun, 2008 - 05:04 PM
Post #5

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 3

QUOTE(mikeblas @ 1 Jun, 2008 - 09:18 AM) *

The error message means what it says. When you use a subscript on a variable, that variable has to be a pointer or an array. You're using a subscript on the variable name in your DisplayName() function, but name is not a pointer--it's a scalar integer.

I can't quite figure out what it is you're trying to do in the DisplayName() function. What's the goal of your project?


Thanks. That makes sense. I fixed the error. The goal is to prompt the user for last name, first name and middle name. Create a dynamically allocated array, sort the names in order and delete the array, then use a loop to continue until the user wishes to exit. I did some more research and I was able to figure out the problem. But, thanks for your help smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month