Hi, I was working on my assignment and on compiling everything works perfect except at one place the compiler seems to skip the cin command.

Here is my code: the skipped command is highlited in red. This particular part is suppose to delete the every occurence of an integer entered by the user.

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

typedef int* ArrayPtr;//creates a pointer for dynamic arrays.

void Erase(int [], int* ,int* );


int main()
{
	char choice;

	int itemCount= 0, SearchElement=0; //itemCount keeps track of the size, SearchElement is for the element needs to be deleted.

	cout<<"You will be creating an array of integers. Please input the size of the array."<<endl;
	cin>> itemCount;

	ArrayPtr table; //declares table a pointer of defined type
	table = new int[itemCount]; // creates a dynamic array called table
    

	cout<<"Please select from the following menu."<<endl;
	cout<<"	E(Erase Array Content)\tC(Count Words)\tR(Reverse Words)\tQ(Quit)"<<endl;
	cin>> choice;
	cin.ignore();

	switch(choice)
		case 'E':
		case 'e':
			Erase(table, &itemCount, &SearchElement); 
			//for(int index=0; index < itemCount; index++)
 //if (table[index] == SearchElement)
	 
cout<<"The size of the array is: "<<itemCount<<endl;

	return 0;
}

void Erase( int ar[], int *item, int *Search)// this function is suppose to delete every occurrence of the digit input by the user.the item is suppose to keep the count of the array size  search is the item to be deleted.
{

cout<<"Please enter \t"<<*item<<" \t integers"<<endl;
for(int index=0; index < *item; index++)
cin>> ar[index];

cout<<"You can now select a number from the array whose all occurences can be deleted \n.";
cout<<"Please select number to be deleted from the array."<<endl;

cin>> *Search;// suppose to store the user input for the integer to be deleted.
for(int index=0; index<*item; index++)
if(ar[index]== *Search)
 *item--;

return;
}

Recommended Answers

All 6 Replies

That's wrong expression statement:

*item--;

It treated as *(item--) - see C++ operator priority table (or better write parentheses).
So the program change a pointer only.
For in/out parameters use references:

void Erase( int ar[], int& item, int7 Search)
...
item--; // ok, decrement passed by reference argument

A compiler never "skip" codes (except in conditional compilation blocks #if ... #endif),
cin is not a command, it's a name of standard input stream...

You don't understand a role of the 1st generated by Visual Studio directive:

#include "stdafx.h"

Place all system header includes like <iostream> or <string> in VS generated header file stdafx.h, not in main module (or other your modules)! Read about precompiled headers in VS help.

That's wrong expression statement:

*item--;

It treated as *(item--) - see C++ operator priority table (or better write parentheses).
So the program change a pointer only.
For in/out parameters use references:

void Erase( int ar[], int& item, int7 Search)
...
item--; // ok, decrement passed by reference argument

A compiler never "skip" codes (except in conditional compilation blocks #if ... #endif),
cin is not a command, it's a name of standard input stream...

You don't understand a role of the 1st generated by Visual Studio directive:

#include "stdafx.h"

Place all system header includes like <iostream> or <string> in VS generated header file stdafx.h, not in main module (or other your modules)! Read about precompiled headers in VS help.

Sorry, but I don't get what exactly should I do to get the compiler to read in the cin stream.
Please can you elaborate on that

The compiler DOES NOR READ the cin stream. It compiles your program source, translate it from C++ to CPU instructions. After that a linker binds the compiled code with run-time library modules - it's so called executable module (a file with exe extension). Your PROGRAM (the same exe module) runs and reads the cin stream.

I see you are yet another copypaster so simply change wrong statement *item--; to (*item)--;

The compiler DOES NOR READ the cin stream. It compiles your program source, translate it from C++ to CPU instructions. After that a linker binds the compiled code with run-time library modules - it's so called executable module (a file with exe extension). Your PROGRAM (the same exe module) runs and reads the cin stream.

I see you are yet another copypaster so simply change wrong statement *item--; to (*item)--;

"Copypaster"? Excuse me, being a beginner, its not a big deal if I made a mistake. I wrote the program the way I understood. I did not copy paste it from anywhere. So if u know something please explain it rather then calling out names.

And also the paranthesis: (*item)-- does not make any difference coz I tried doing that.

It does. I have run your program with debugger (and you? ). Correced Erase function modifies itemCount variable with awkward (*item)--. It's the other story that you do not modify the array, you only count elements after intended array rebuilding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.