Oops, left out some parenthesis... here you go... this is the line to add...
invList[i].itemName = new char[ strlen(name.c_str())+1 ];
Main Topics
Browse All TopicsI have a little routine
void CFoodStr::TruckArr(){
string name;
cout << "\nENTER CONTENTS\n";
for(int i=0; i<20; i++)
{
cout <<"ITEM # "<<i<<endl;
cout <<"\n\n\nITEM NAME :";
cin >> name;
strcpy(invList[i].itemName
}
}
//this is my header file
struct TFoodItem
{
char* itemName;
int price;
int quant;
};
struct TCustomer
{
char* custName;
TFoodItem shpList[20];
};
class CFoodStr
{
public:
CFoodStr();
void TruckArr();
void custArr(string);
void checkOut(int);
private:
TFoodItem invList[20];
TCustomer shpCart[10];
};
Strcpy bombs
what am I missing?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
//great responses, and thank you all!
//i'm a novice at c++, and they talk about strings, but everything i see in the
//book is
char string1[]= ""; etc
//a couple of followups
//smitty1276>>that line worked, thanks, but and I am going to try to implement burcarpat's suggestion.
//burcarpat>> actually tried to define as strings, and used
#include <string>
//but just noticed I forgot
using std::string;
//how should my struct look?
string itemName;//?
I am going to go back at this in the morning!
thanks again
here's a working version of your code, implemented using std::string:
-- cut from here ---
// c/c++
#include <iostream>
#include <string>
struct TFoodItem {
std::string itemName;
int price;
int quant;
}; // TFoodItem
struct TCustomer {
std::string custName;
TFoodItem shpList[20];
}; // TCustomer
class CFoodStr {
public:
CFoodStr() { };
void TruckArr();
void custArr(std::string);
void checkOut(int);
private:
TFoodItem invList[20];
TCustomer shpCart[10];
}; // CFoodStr
void
CFoodStr::TruckArr() {
std::string name;
std::cout << "\nENTER CONTENTS\n";
for(int i = 0; i < 20; ++i) {
std::cout <<"ITEM # "<< i << std::endl;
std::cout <<"\n\n\nITEM NAME :";
std::cin >> name;
invList[i].itemName = name;
};
}; // CFoodStr::TruckArr
int
main(int _argc,
char* _argv[]) {
CFoodStr FoodStr;
FoodStr.TruckArr();
return 0;
}; // main
-- cut from here ---
i suggest, you always use the std:: prefix when referring to standard library. that way, you don't need any using statements. using statements clutter the global namespace and thus should generally be avoided
also, you might want to use std::vector instead of an array. in this case, you don't need it but it would be a good exercise. std::vector is capable of doing everything that a c-array can do and them some
btw, i strongly suggest you get a better c++ book if your book does not explain std::string properly. stroustrup's (the c++ programming language) or josuttis' (the standard c++ library) books will do just fine
-- ba
Your problem is a bufferoverflow! Use strncpy()
>invList[i].itemName = new char[ strlen(name.c_str())+1 ];
Not a good idea! An attacker can input a 2GB string and your application will shutdown...
Just use:
// Set the length of the max input!
#define MAX_INPUT 100
invList[i].itemName = new char[ MAX_INPUT+1];
ZeroMemory(invList[i].item
// No bufferoverflows now
strncpy(reinterpret_cast<c
//....
// don forget to free
delete [] invList[i].itemName;
But you have to set the size of invList[i].itemName to a value that is large enough for you!
I hope this helps you
Additonal you should use:
char* name=""
cin>> name;
// inizialize
invList[i].itemName = new char[ MAX_INPUT+1];
ZeroMemory(invList[i].item
// No bufferoverflows now
strncpy(reinterpret_cast<c
// befor terminating the class
for(int i=0;i<=invList[i].Count-1;
{
delete [] invList[i].itemName;
}
Additonal you should use:
char* name=""
cin>> name;
// inizialize
invList[i].itemName = new char[ MAX_INPUT+1];
ZeroMemory(invList[i].item
// No bufferoverflows now
strncpy(reinterpret_cast<c
// befor terminating the class
for(int i=0;i<=invList[i].Count-1;
{
delete [] invList[i].itemName;
}
I firmly believe that if someone is still learning the language they should become as proficient as possible using the methods upon which the std classes were implemented. Yeah, the string and vector classes are great and all, but they were written using char* and the like.
As a rule, if anyone is having trouble understanding a basic old fashioned char* type string, then they shouldn't be using the std::string yet.
Business Accounts
Answer for Membership
by: smitty1276Posted on 2003-08-17 at 21:02:01ID: 9171086
You probably never allocated the memory for the itemName member of TFoodItem
Add this line before the line that bombs...
//allocate space to UNITIALIZED POINTER for the name... make sure to add
//one extra space at end for the null (strlen doesn't count the null)
invList[i].itemName = new char[ strlen(name.c_str) + 1 ];
Alternatively, you could get the strings length using the string class's length function.