why do i get this error?

D

Developwebsites

#include<iostream.h>
#include<iomanip.h>
#include<string>

string names[numb_names];

for(int k=1;k<=numb_names;k++) {
cout<<"\nplease enter name#"<<k<<": ";
cin>>names[k];
}

for(int k=1;k<=numb_names;k++) {
cout<<"\nname#"<<k<<"is: "<<names[k];
}

I get:
"this program has performed an illegal operation"
and the console closes.

except when 14 names are entered.
 
A

Artie Gold

Developwebsites said:
#include<iostream.h>
Non-standard header. Use said:
#include<iomanip.h>

Non-standard header. Use said:
#include<string>

using namespace std; // only because it's a small program
string names[numb_names];

What's `numb_names'? It's not defined anywhere.

Assuming `numb_names' *is* defined, `names' is an array of strings
whose valid indices run from 0 to numb_names-1.
for(int k=1;k<=numb_names;k++) {
cout<<"\nplease enter name#"<<k<<": ";
cin>>names[k];

When `k' == `numb_names', you've exceeded the bounds of your array
of strings. *Undefined behavior* (or, in this case, BOOM!).
}

for(int k=1;k<=numb_names;k++) {
cout<<"\nname#"<<k<<"is: "<<names[k];
Similarly.

}

I get:
"this program has performed an illegal operation"
and the console closes.

except when 14 names are entered.

HTH,
--ag
 
C

Chris Johnson

#include<iostream.h>
#include<iomanip.h>
#include<string>

string names[numb_names];

for(int k=1;k<=numb_names;k++) {
cout<<"\nplease enter name#"<<k<<": ";
cin>>names[k];
}

for(int k=1;k<=numb_names;k++) {
cout<<"\nname#"<<k<<"is: "<<names[k];
}

I get:
"this program has performed an illegal operation"
and the console closes.

except when 14 names are entered.

You are indexing an array out of bounds.

You do know that when you declare:
const int MAGIC_NUM = 10;
string names[MAGIC_NUM]; // names[0] ... names[9]

If you exceed an array bound you will get
undefined behavior - most noticably a sigfault.
 
G

Gianni Mariani

Developwebsites said:
#include<iostream.h>

Use #include said:
#include<iomanip.h>

Use #include said:
#include<string>

That's more like it.
string names[numb_names];

for(int k=1;k<=numb_names;k++) {

for(int k=0;k<numb_names;k++) {
cout<<"\nplease enter name#"<<k<<": ";
cin>>names[k];
}

for(int k=1;k<=numb_names;k++) {

for(int k=0;k<numb_names;k++) {
cout<<"\nname#"<<k<<"is: "<<names[k];
}

I get:
"this program has performed an illegal operation"
and the console closes.

except when 14 names are entered.

arrays in C++ (and C) go from 0...N-1, your code assumes that the
indexes are from 1..N.


Your code bailed because you indexed too far and "undefined" things
happened.
 
S

Shane Beasley

I get the same error when using:
char *city_name[numb_cities];

What does this program do?

int x;
std::cout << x;

The correct answer is, "I don't know." Nobody stored an integer into
x, so nobody can say what integer is stored there. It may output
whatever number happened to be in the memory occupied by x at the
time. (ISO C++ says it may do anything, even crash.)

Similarly, you can't know what this program does, either:

char *p;
cin >> p;

Nobody stored an address into p, so nobody can say what address is
stored there. Yet cin will start storing user input at that address,
whatever it may be. The behavior of such a program is undefined. If
you're lucky, it'll try to overwrite memory to which you can't write
or memory containing important data, either crashing or distorting
your output in the process. If you're not, it may appear as though it
is "working" -- which it's not, since a program that writes data to
random areas of memory can't possibly be working properly.

Pointers are only useful when they actually point somewhere -- in this
case, a a char * must point into an array of char to be used as you
wish to use it. The array must be of suitable size, and you must take
care not to read outside of that size. It should be deallocated when
you're done (i.e., not leaked), and only deallocated once, and not
used again after being deallocated.

Or you could just use std::string, which does all this for you. But
you knew that. :)

- Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top