Whats wrong with this?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1183 Views - Last Post: 07 December 2010 - 12:54 PM Rate Topic: -----

#1 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Whats wrong with this?

Posted 06 December 2010 - 07:32 PM

#include <stdio.h>
#include<iostream>
#include<vector>
#include<fstream>
#include<string>

int main(int argc, char** argv)
{
  	
	// Declarations
	std::string str;
	std::vector<std::string> myVector;
	std::string inputString=new string (argv[1]);
	
	
	// Open file and stores words in a vector
	std::ifstream myFile;
	myFile.open (argv[2]);


	while (myFile.good()){
		std::getline(myFile,str);
		myVector.push_back(str);
	}
	myFile.close();


	// Ask for input string from user and convert to upper case
	//std::cout << "Please enter a word to search" << std::endl;
	//std::cin >> inputString;
	//inputString = argv[1];


	for(int j=0;j<inputString.length();j++){
		inputString[j] = toupper(inputString[j]);
	}



	// Check if word is in the Vector and return count as line number
	// if word is not in the list the bool will remain false.
	// The bool is checked after the loop
	
	bool wordInList=false;
	int lineCount=1;															// Start at line one								
	for(int i=0;i<myVector.size();i++){			
		if(inputString == myVector[i]){
			std::cout << lineCount << std::endl;
			wordInList=true;
		}
	lineCount++;
	}

	if(wordInList == false){
		std::cout << 0 << std::endl;
	}
}




I have messed with it slightly. Basically it works fine on my machine. Takes an input file names myfile.txt from my machine and then a word from the user and outputs the number of line the word is on or 0 if word isnt there.

My program works fine, however....

On a submission website obviously I cant use myFile.txt from my machine and same with inputting a word. So I use argc and argv pointers as seen above. Am I using them wrong or what?

This silly website keeps telling me im wrong and it aint working :(

This post has been edited by Kohana: 06 December 2010 - 07:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Whats wrong with this?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Whats wrong with this?

Posted 06 December 2010 - 07:45 PM

If the input filename is argv[2], what's argv[1]?

If there's only supposed to be 1 argument (the input filename) that would be argv[1]. The name of the program is in argv[0].
Was This Post Helpful? 1
  • +
  • -

#3 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 07:54 PM

View Postr.stiltskin, on 06 December 2010 - 06:45 PM, said:

If the input filename is argv[2], what's argv[1]?

If there's only supposed to be 1 argument (the input filename) that would be argv[1]. The name of the program is in argv[0].


Sorry matey, I changed the input string to argv[1]. Thanks for looking, any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 08:05 PM

Revised code

#include <stdio.h>
#include<iostream>
#include<vector>
#include<fstream>
#include<string>

int main(int argc, char* argv[])
{
  	
	// Declarations
	std::string str;
	std::vector<std::string> myVector;
	std::string inputString;
	
	inputString=argv[1];
	// Open file and stores words in a vector
	std::ifstream myFile;
	myFile.open (argv[2]);


	while (myFile.good()){
		std::getline(myFile,str);
		myVector.push_back(str);
	}
	myFile.close();


	// Ask for input string from user and convert to upper case
	//std::cout << "Please enter a word to search" << std::endl;
	//std::cin >> inputString;
	//inputString = argv[1];


	for(int j=0;j<inputString.length();j++){
		inputString[j] = toupper(inputString[j]);
	}



	// Check if word is in the Vector and return count as line number
	// if word is not in the list the bool will remain false.
	// The bool is checked after the loop
	
	bool wordInList=false;
	int lineCount=1;															// Start at line one								
	for(int i=0;i<myVector.size();i++){			
		if(inputString == myVector[i]){
			std::cout << lineCount << std::endl;
			wordInList=true;
		}
	lineCount++;
	}

	if(wordInList == false){
		std::cout << 0 << std::endl;
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Whats wrong with this?

Posted 06 December 2010 - 08:21 PM

They're right -- it doesn't work, and it won't work on your machine either.

This post has been edited by r.stiltskin: 06 December 2010 - 08:28 PM

Was This Post Helpful? 1
  • +
  • -

#6 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 08:29 PM

any idea why?

Its not for academic purposes. Infact I am a final year game student. I dont know why Im only learning about this argc and argv now. Its silly.

If you check my profile and posts this is the first time I have asked for help and have offered it plenty of times. I even have my work up on the challenge pages for c++ and openGL. My point, your not giving an answer to an academic paper. This is a personal quest :)

Thanks again

Lew

edit. The code on my machine is different. IE I read from my own file and I get a word inputted from user. Some parts of that code are commented out as you can see. Works fine like that but when I replace the myFile.txt with argv[2] and the inputted word from user with argv[1] it falls over.

This post has been edited by Kohana: 06 December 2010 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Whats wrong with this?

Posted 06 December 2010 - 08:32 PM

It has nothing to do with argc and argv (well, except for line 18 & you should be able to figure that out as soon as you look at it).

Why don't you do a little testing: print what you're putting into the vector, and print your search string just before the searching loop.
Was This Post Helpful? 1
  • +
  • -

#8 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 08:35 PM

View Postr.stiltskin, on 06 December 2010 - 07:21 PM, said:

They're right -- it doesn't work, and it won't work on your machine either.


Here is the code I have on my machine before I attempted input from console via argv's

Try it your self, of course it works on my machine. It'll work on yours too. Fire up VS and try it.

#include<iostream>
#include<vector>
#include<fstream>
#include<string>

void main(int argc, char argv[]){
	
	// Declarations
	std::string str;
	std::vector<std::string> myVector;
	std::string inputString;

	// Open file and stores words in a vector
	std::ifstream myFile;
	myFile.open ("myFile.txt");


	while (myFile.good()){
		std::getline(myFile,str);
		myVector.push_back(str);
	}
	myFile.close();


	// Ask for input string from user and convert to upper case
	std::cout << "Please enter a word to search" << std::endl;
	std::cin >> inputString;


	for(int j=0;j<inputString.length();j++){
		inputString[j] = toupper(inputString[j]);
	}



	// Check if word is in the Vector and return count as line number
	// if word is not in the list the bool will remain false.
	// The bool is checked after the loop
	
	bool wordInList=false;
	int lineCount=1;															// Start at line one								
	for(int i=0;i<myVector.size();i++){			
		if(inputString == myVector[i]){
			std::cout << lineCount << std::endl;
			wordInList=true;
		}
	lineCount++;
	}

	if(wordInList == false){
		std::cout << 0 << std::endl;
	}

	// Pause for user to see output
	system("PAUSE");
}



My input file is like this, one word per line in a text file. Name the file myFile.txt

HELLO
WORLD
MY
NAME
IS
LEWIS
JENKINS
FROM
THE
UNITED
KINGDOM
Was This Post Helpful? 0
  • +
  • -

#9 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 08:48 PM

Current Submission,

Error = Your submission failed to give the exact answer on the verification test. Please verify your code.
For reference, you program generated these results:
0

#include<iostream>
#include<vector>
#include<fstream>
#include<string>

int main(int argc, char **argv){
	
	// Declarations
	std::string str;
	std::vector<std::string> myVector;
	std::string inputString=argv[1];

	// Open file and stores words in a vector
	std::ifstream myFile(argv[2]);
	myFile.open("argv[2]");     // tried with and without passing argv[2]


	while (myFile.good()){
		std::getline(myFile,str);
		myVector.push_back(str);
	}
	myFile.close();


	// Ask for input string from user and convert to upper case
	//std::cout << "Please enter a word to search" << std::endl;
	//std::cin >> inputString;


	for(int j=0;j<inputString.length();j++){
		inputString[j] = toupper(inputString[j]);
	}



	// Check if word is in the Vector and return count as line number
	// if word is not in the list the bool will remain false.
	// The bool is checked after the loop
	
	bool wordInList=false;
	int lineCount=1;															// Start at line one								
	for(int i=0;i<myVector.size();i++){			
		if(inputString == myVector[i]){
			std::cout << lineCount << std::endl;
			wordInList=true;
		}
	lineCount++;
	}

	if(wordInList == false){
		std::cout << 0 << std::endl;
	}

}




This post has been edited by Kohana: 06 December 2010 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Whats wrong with this?

Posted 06 December 2010 - 09:00 PM

OK, it works. It's very fragile, though. It works only if the input file consists of all uppercase letters, and only 1 string per line. Under those conditions, your new code (in post #4) works too. So what's your question?

This post has been edited by r.stiltskin: 06 December 2010 - 09:00 PM

Was This Post Helpful? 1
  • +
  • -

#11 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 09:04 PM

View Postr.stiltskin, on 06 December 2010 - 08:00 PM, said:

OK, it works. It's very fragile, though. It works only if the input file consists of all uppercase letters, and only 1 string per line. Under those conditions, your new code (in post #4) works too. So what's your question?



The input file for the task is in the format, all upper case and 1 word per line. So thats how its supposed to be.

Right see my new code? I am trying to submit it to a website where obviously it needs the console inputs but it keeps throwing me errors :(

Try it......

http://yodacode.com/...7/source_editor

.......I have gone beyond caring about the submission so feel free to take it and try and submit it yourself. Now I just wanna put my own demons to rest and make this work. There is deffo something I am missing.

I hate this automatic marking programs.

cheers for this matey
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Whats wrong with this?

Posted 06 December 2010 - 09:15 PM

Based on what I see there, they provide the test input file, right? So why do you conclude that it's going to be all uppercase letters?

Also, it says that you are supposed to do a case-sensitive match. Not only are you not doing that, but by converting the search string to uppercase, you're throwing away all information about the case of the search string.
Was This Post Helpful? 1
  • +
  • -

#13 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 09:19 PM

View Postr.stiltskin, on 06 December 2010 - 08:15 PM, said:

Based on what I see there, they provide the test input file, right? So why do you conclude that it's going to be all uppercase letters?

Also, it says that you are supposed to do a case-sensitive match. Not only are you not doing that, but by converting the search string to uppercase, you're throwing away all information about the case of the search string.


They tell you the file is all uppercase strings. Hence the conversion
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Whats wrong with this?

Posted 06 December 2010 - 09:25 PM

Where does it say that? All I see on that website is this:

Quote

The goal of this puzzle is to find the first line that contains a given word and to print its number on the standard output. If no line is found that contains the given word, the value 0 should be printed. The match should be case-sensitive (the word must match the same case as the input-argument's case).

Input Specifications

Your program must take 2 arguments:

1. the word to look for
2. the input file in which to search for the word

The input file contains only a word per line. For example, let this be myfile.txt:

Hello
World


The example they give is not all uppercase.
Was This Post Helpful? 1
  • +
  • -

#15 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Whats wrong with this?

Posted 06 December 2010 - 09:28 PM

View Postr.stiltskin, on 06 December 2010 - 08:25 PM, said:

Where does it say that? All I see on that website is this:

Quote

The goal of this puzzle is to find the first line that contains a given word and to print its number on the standard output. If no line is found that contains the given word, the value 0 should be printed. The match should be case-sensitive (the word must match the same case as the input-argument's case).

Input Specifications

Your program must take 2 arguments:

1. the word to look for
2. the input file in which to search for the word

The input file contains only a word per line. For example, let this be myfile.txt:

Hello
World


The example they give is not all uppercase.


Your right man haha silly me.however I just removed the uppercase logic and still not accepting it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2