Jon Rurka
Jon Rurka

Reputation: 79

C++ console application acting up

I am very new to programming, and have been following a C++ tutorial for the past week or so. Today, I decided to make my own console application without help to make sure I have all the concept down I have read so far. The application asks you in the beginning for 5 different words to put into a 5 element array. You are then prompted by a menu giving you 4 options: print the contents of the array, re-enter all the words again, edit only one array element, or exit the app.

When I choose the "edit only one array" option, the application gives another menu asking you to press "1" to print the array, or press any other key to just go on editing. The straight-to-editing option leads to a mashed up version of the start of the program, and using the "1" option leads to just chaos: https://i.sstatic.net/bFLLz.jpg. The console continues to print objects on the screen similar to the ones in the picture for about 5 seconds, until I am prompted by a popup asking me to end the process. I receive this in the output log after the application closes: pastebin.com/KcRU56Tg

The source code for my application can be found here: http://pastebin.com/vRUddYuK. I would normally attempt to debug this myself (and have been for the past hour and a half), but I feel that this is way over my head. Any help will be appreciated. Thank you.

Upvotes: 1

Views: 146

Answers (3)

michael henson
michael henson

Reputation: 158

do { ... } while statements should be avoided when doing iterations. It's too easy to make logic errors like you did.

Note that PrintInv is executing it's cout statement when n == 5, this is an error since inv only has valid elements for indices 0...4.

Rewrite your iterations with

for(int i=0;i < 5;++i)

and your logic will be clearer and bugs will be less likely to happen.

Upvotes: 3

philipvr
philipvr

Reputation: 6368

Your variables i and n are redundant; you only need one of these variable.

Your problem occurs when you call your PrintInv function with n equal to 5, and your PrintInv print values of the array that past the end of the array so therefore you are seeing the garbage values being printed.

You should change your print function to look something like this:

// pass the array and the size of the array to your print function
void printInv(string inv[], int arrSize) {
   for(int i = 0; i < arrSize; ++i) { 
      cout << "Item " << (i + 1) << ": " << inv[i] << endl;
   }
}

and invoke the function like this:

printInv(inv, 5);

Also, variables should be declare within the scope they are needed. You do not need to declare every variable at the beginning of your main function.

Upvotes: 0

Eran
Eran

Reputation: 22020

You seem to be passing an n that is equal to 5 to PrintInv. Then, you access inv[n], which is inv[5], which goes beyond the bounds of your 5 element array. Unfortunately, this doesn't cause your program to crash. Instead, it just dumps a lot of memory, until it happens to come by a random NULL.

Upvotes: 2

Related Questions