ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi, i am new to c++.i am trying to read from a file and put the information into an array and then display the contents of the array to the screen. i do not get any error when i run this project from eclipse and when i run it from Microsoft Visual Studio it says that the riders.exe has stopped working. can someone please take a look and tell me why its not working.
Can you verify whether the file is being opened or not? Heres a part of your "main", with irrelevant details left out
Code:
Motorcycles array[10];
if(bikes.is_open()){
// ...
}
for (int x = 0; x < 10 ; x++){
array[x].toString();
}
As you can see, the last block (the for loop) always gets executed. If the file failed to open, then the "array" array is not initialized, and youll get a segfault/null pointer exception on the coloured line. This for loop should probably be inside the if block, so you only print the elements if the file was opened and read successfully.
Also, aside, I would name the class "Motorcycle"--singular--opposed to "Motorcycles", because the latter is misleading.
when i run it from Microsoft Visual Studio it says that the riders.exe has stopped working.
It didn't say exactly that did it?
I think you are incorrectly paraphrasing a message that you misunderstood.
I suspect that your problem is in misuse of Visual Studio rather than in your program itself.
Visual Studio can be very confusing to beginners.
If you are a beginner at Visual Studio, tell us more details about version of Visual Studio and about what specific actions you took to build your program and to run it and to get a look at the output from running it (which can be surprisingly hard in Visual Studio).
Of course if you are not a beginner at Visual Studio, I probably guessed wrong and also missed some bug in your source code. So if you are confident in your ability to operate Visual Studio, tell us that.
this is not my first time with visual studio, i think i can create projects the right way. i added a line in the main()'s if loop. the line executes but it never goes to the for loop. i have attached a screen shot of what i get when i run it.
Code:
if(bikes.is_open()){
cout << "Cannot open file"<<endl;
for ( int i = 0; getline(bikes,binfo); i++){
//istringstream iss (binfo);
array[i] = getObj(binfo);
//cout << array[i] << endl;
}
for (int x = 0; x < 10 ; x++){
array[x].toString();
}
}
You put the "Cannot open file" message in the path where is has opened the file.
The message I didn't believe is from Windows, not from Visual Studio. I just use XP, not the newer stupider versions of Windows, so I didn't recognize that message.
Anyway, it seems there is a bug in your source code.
Earlier I didn't notice your loop depends on the contents of the file and is not limited to the ten items for which you have room in the array. So if the loop went more than ten times, that would exactly fit the symptoms.
Maybe you accidentally got a blank line at the end of bikes.txt, maybe in converting to a Microsoft environment.
The way you coded your program, an extra line at the end of bikes.txt would cause the crash.
Also (but not related to the crash) it is very bad style to have a function that is a slight misspelling of the class name
Code:
void Motorcycles::Motorcylces
The way you used that, it would have been better to have a non default constructor:
But if you really want an init function instead of a constructor, it is much better to give it a name such as init, rather than misspell the constructor name.
my bad, i didn't realize i misspelled the names. that was not my intention. although i fixed all the errors and changed to your suggestions i still get that Bikes.exe error.
if(bikes.is_open()){
cout << "Cannot open file"<<endl;
This message is obviously misleading--it was able to open the file! Anyway, the easiest thing to do is to put print statements (or use a debugger) throughout your code, and find out the last message that printed before an error occurred. Once you found this, you found the exact line the error occurs on. Then you can tell us the line it occurred on.
Code:
if(bikes.is_open()){
cout << "here 1 "<<endl;
for ( int i = 0; getline(bikes,binfo); i++){
cout << "here 2 "<<endl;
//istringstream iss (binfo);
array[i] = getObj(binfo);
cout << "here 3 "<<endl;
//cout << array[i] << endl;
}
cout << "here 3 "<<endl;
for (int x = 0; x < 10 ; x++){
cout << "here 4 "<<endl;
array[x].toString();
cout << "here 5 "<<endl;
}
cout << "here 6 "<<endl;
}
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by johnsfine
It didn't say exactly that did it?
I think you are incorrectly paraphrasing a message that you misunderstood.
I suspect that your problem is in misuse of Visual Studio rather than in your program itself.
Visual Studio can be very confusing to beginners.
If you are a beginner at Visual Studio, tell us more details about version of Visual Studio and about what specific actions you took to build your program and to run it and to get a look at the output from running it (which can be surprisingly hard in Visual Studio).
Of course if you are not a beginner at Visual Studio, I probably guessed wrong and also missed some bug in your source code. So if you are confident in your ability to operate Visual Studio, tell us that.
No, its a runtime not a compile time error.
Ether a memory viloation or illegal instruction.
Last edited by smeezekitty; 11-04-2009 at 12:26 PM.
apart from my misspelling there was nothing wrong with the code. the professor had put tabs and extra spaces in the file, because he wanted us to learn the hard way that istringstream doesnt like tabs and extra line breaks.
this is the code that is working.
We are way past that. I didn't believe a "Visual Studio" message was quoted correctly. It turns out it was quoted correctly but wasn't a Visual Studio message (it was a Windows message). Simple misunderstanding, now understood, no longer a problem.
My current working theory is that the failure is due to the line
Code:
array[i] = getObj(binfo);
executed when i is 10, because i is only allocated for 0..9.
We are waiting for tironci to confirm or contradict that theory and/or follow nadroj's advice to add so many printf's that the output could confirm "here 2" happens more than ten times, or if that is incorrect, would likely nail down the real bug.
If you have another theory about the real bug or other constructive suggestion, that would be interesting.
But meanwhile, a post such as you just made (excess quoting and useless extra comment) just serves to make it harder to find the constructive posts. I'm sorry this reply is impolite. I'd prefer not to be. But I think it needed to be said.
apart from my misspelling there was nothing wrong with the code. the professor had put tabs and extra spaces in the file, because he wanted us to learn the hard way that istringstream doesnt like tabs and extra line breaks.
this is the code that is working.
I'm not your instructor, so I'm only guessing, but maybe he also wanted you to learn some defensive programming.
Is code that crashes when the input is bad "wrong" or is the input the only thing that is "wrong" in that case?
That is a matter of opinion and a matter of degree.
Sometimes it just isn't practical to defend against all possible bad input. So I would not accept the absolute rule (which I have frequently seen) that the program is "wrong" if there is any input that makes it crash.
But almost any experienced programmer would say your program is "wrong", because crashing when the input file has more than ten lines is too severe for a simple input error and too easy to fix with a minor change to your source code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.