Thread: c++ code not compiling in online compiler?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    c++ code not compiling in online compiler?

    Hey guys,

    Its a bit urgent..

    please see the code below:
    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    int main()
    {
    	int n,i,print_count=0,word_count=0,j,string_length,index,match,different=0;
    	char *word; //variable to hold the word to be removed.
    	char current_char,first; //current_char holds the letters in the word_array one_by_one
    	
    	cout<<endl<<"PLEASE ENTER THE WORD TO BE REMOVED FROM WORD_ARRAY"<<endl;
    	gets(word);
    	
    	//cout<<endl<<word;
    	//cout<<endl<<strlen(word);
    	
    	string_length=strlen(word); //length of string to be removed
    	first=word[0]; //first holds first char of word
    	
    	cout<<endl<<"PLEASE ENTER NUMBER OF WORDS IN WORD_ARRAY"<<endl;
    	cin>>word_count;
    	
    	if(word_count<=0) //validates the input
    	{
    		cout<<endl<<"PLEASE NOTE THAT WORD COUNT SHOULD BE DIGIT GRETAER THAN ZERO";
    	}
    	else
    	{
    	
    		char word_array[word_count][200]; //if valid input,dynamically create 2D word array
    	
    		cout<<endl<<"PLEASE ENTER THE WORDS IN THE WORD_ARRAY ONE BY ONE"<<endl;
    	
    		for(i=0;i<word_count;i++)
    		{
    			cout<<endl<<"ENTER WORD NUMBER "<<i+1<<">>"<<endl; //read the words into the 2D array
    			gets(word_array[i]); 
    		}
    	
    		/*for(i=0;i<word_count;i++)
    		{
    			cout<<endl<<word_array[i];
    		}*/
    		//cout<<endl<<"here it goes"<<endl;
    		cout<<endl<<endl<<"Output:"<<endl<<endl;
    		cout<<"["; //square bracket at beginning of output
    	
    		for(i=0;i<word_count;i++)   // loop to go through each word of 2d word_array
    		{
    			cout<<"\""; //double quotes before each word in output
    			for(j=0;word_array[i][j]!='\0';) // loop to check the last character of current word 
    			{
    				different=0;
    				current_char=word_array[i][j]; // set current_char
    				//cout<<endl<<"current char="<<current_char<<endl;
    				if(tolower(current_char)==tolower(first))	 //if there is a first letter match,then only go to letter removal logic
    				{
    					//cout<<endl<<"first char match"<<endl;
    					index=0; // index increments through each letter of 'word' variable. set it to zero,each time ther is a 1st letter match.
    					match=0;
    					while(index<=(string_length-1)) // loop till index reaches the last character
    					{
    						if(tolower(word[index])==tolower(word_array[i][j])) //if there is a match in letters,go inside if block
    						{
    							//cout<<endl<<"hurray";
    							index++; //in case of match,make index point to next char of word
    							match++; //if a match,increment no of matched chars.
    							j++; //in case of match,point to next char of string in word_array
    							//cout<<endl<<"match="<<match;
    						}
    						else
    							break; //if no match,get out of loop
    					}
    					if(match!=string_length) //if the entire word  does not match,move the j index back to the first letter of partially matched word
    					{
    						//cout<<endl<<endl<<"j="<<j;
    						different=1;
    						j=j-match;
    					}
    				}
    				
    				/***
    				A 'different' variable is used here so that even when the word to be removed appears continiously 
    				in the input array,it will be removed by the code below.At the same time,it will also print partially 
    				matched words.
    				***/
    			
    			//(tolower (word_array[i][j]))!=tolower(first) checks if word which immediately follows the previous word also has to be removed.
    			//The condition (different==1) ensures that partially matched words will not be skipped due to the condition tolower (word_array[i][j]))!=tolower(first) 
    				if(word_array[i][j]!='\0'&& ((tolower (word_array[i][j]))!=tolower(first) || (different==1))) 
    				{
    				//cout<<endl<<"not null";
    					cout<<word_array[i][j]; //if not a part of given word,print onto screen
    					j++; //increment and check next char
    				}
    			}
    			cout<<"\""; //double quotes before each word in output
    			//cout<<print_count++;
    			if(i<word_count-1)
    			cout<<",";
    			
    			
    		}
    	
    	cout<<"]"; //square bracket at end of output
    	}
    }
    this code compiles and run in the c- Free 4.0 compiler but in the online comipler ideone, it was giving a compilation error . so i googled about it and changed iostream.h to iostream and added using namespace std;

    but now its giving a runtime error. is it actually possible to run this program online?
    Last edited by theju112; 10-09-2012 at 11:47 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean with this "worlds worst" coding mistake?

    Code:
    	char *word; //variable to hold the word to be removed.
    ///
    	gets(word);
    1. You have an uninitialised pointer, so you just spray characters over some random bit of memory. Using the online tool, you win; Using c-free, you lose.
    2. You use gets()
    SourceForge.net: Gets - cpwiki

    > char word_array[word_count][200]; //if valid input,dynamically create 2D word array
    Variable length arrays are only officially supported in C99.

    For a C++ program, there is an awful lot of C in there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Salem View Post
    For a C++ program, there is an awful lot of C in there.
    It's not really C++ at all.
    I call it "C with cout".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    tried out so many things but just couldn't avoid the run time errors and segmentation faults...

    so i chose java and it finally compiled..

    heres the code anyway: please move it if pasting java code here is inappropriate

    Code:
    import java.util.Scanner;
    
     class RemoveWord
    {
    	public static void main(String[] args)
    	{
    		String subStr; //variable to hold the word to be removed.	
    		int numOfWords;
    		
    		
    		Scanner in = new Scanner(System.in);
    		
    				
    		System.out.println("PLEASE ENTER THE WORD TO BE REMOVED");
    		subStr = in.nextLine();
    		
    		
    		
    		System.out.println("PLEASE ENTER THE NUMBER OF WORDS IN THE WORD ARRAY");
    		numOfWords = in.nextInt();
    		//System.out.println(numOfWords);
    		
    		if(numOfWords > 0) //validates input
    		{
    				
    			String[] stringCollection = new String[numOfWords];
    			String[] finalArray = new String[numOfWords];
    		
    		
    			System.out.println("ENTER THE WORDS ONE BY ONE");
    			in.nextLine();
    		
    		
    			for(int i=0; i<numOfWords;i++)
    			{
    				int k=i+1;
    				System.out.println("ENTER WORD NUMBER "+k+" >>>");
    				stringCollection[i] = in.nextLine();
    			//	System.out.println(i);
    			}
    		
    	 		finalArray = remove(stringCollection,subStr);
    		
    			System.out.println("\n\nOUTPUT:");
    			System.out.print("["); //square bracket at beginning of output
    			for(String s:finalArray)
    			{
    				System.out.print("\"");
    				System.out.print(s+"\",");
    			}
    			
    			System.out.print("]"); //square bracket at end of output
    		}
    		else
    		{
    			System.out.println("PLEASE THE ARRAY SIZE SHOULD BE GREATER THAN ZERO");
    		}
    		
    	}
    		
    		
    		
    	public static String[] remove(String[] stringCollection,String subStr)
    	{
    		int finalIndex = 0;
    		int index1=0,index2=0;
    		String curString;
    		String tempString="";
    		String[] finalArray = new String[stringCollection.length];
    		/*for(int i=0;i<numOfWords;i++)
    		{
    			System.out.println(stringCollection[i]);
    		}*/
    		
    		
    			for(int i=0;i<stringCollection.length;i++)
    			{
    				 curString = stringCollection[i];
    				 tempString = curString;
    				while(index1!=-1)
    				{
    					index1 = ignoreCase(tempString).indexOf(ignoreCase(subStr)); //checks if substring is present in main string
    					//System.out.println(index1);
    					if(index1 != -1) // if substring present enter if
    					{
    						tempString = curString.substring(0,index1); //copy from beginning till character before substring
    						//System.out.println("yae!");
    						index2 = index1 + subStr.length();
    						tempString = tempString + curString.substring(index2,curString.length()); //copy from character after substring to end of string
    						
    					}
    					curString = tempString; // final string 
    					
    				}
    				index1 = 0;
    				//System.out.println(curString);
    				finalArray[finalIndex++] = curString; //add final string to array
    			
    			}
    		/*for(String s:finalArray)
    		{
    			System.out.println(s);
    		}*/
    	return finalArray;
    	}
    	
    	public static String ignoreCase(String str) // function to ignore case
    	{
    		return str.toLowerCase();
    	}
    }

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    So, we went from almost C++ to mostly Java?

    That's alarming. I hope that isn't contagious.

    Soma

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by theju112 View Post
    tried out so many things but just couldn't avoid the run time errors and segmentation faults...

    so i chose java and it finally compiled..

    heres the code anyway: please move it if pasting java code here is inappropriate
    That's because you're essentially just using C and unaware of its dangerous constructs. You also make beginner mistakes such as using uninitialized pointers, etc.
    Perhaps you should make your mind as to what language you really want to learn. There is no point in posting Java code here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Visual C++ code using a different compiler
    By CodeKate in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2011, 01:09 AM
  2. is there such thing as a online compiler?
    By Anddos in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2008, 11:46 AM
  3. compiling c code in c++ compiler..???Noob
    By roalme00 in forum C Programming
    Replies: 2
    Last Post: 06-30-2007, 07:25 AM
  4. online compiler for gcc
    By kris.c in forum Tech Board
    Replies: 2
    Last Post: 07-14-2006, 12:02 PM
  5. Online Compiler
    By KrAzY CrAb in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 05-04-2003, 08:09 PM