LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2006, 03:51 PM   #1
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Rep: Reputation: 0
comparison is always true/false


I am getting an error when i try to compare a character to check if has the escape character. But this give me the following error when i run it:

inputfile.c:29:28: warning: unknown escape sequence '\o'
inputfile.c:33:33: warning: unknown escape sequence '\o'

My code is the following:

int main(int argc, char *argv)
{
int const MAX_WORDS = 10000;
int const MAX_WORD_LENGTH = 25;
char words [MAX_WORDS][MAX_WORD_LENGTH+1];
ifstream inputFile;
inputFile.open("WarOfTheWorlds-Ch1.txt");
char a;
int number_words = 0;
for (int i = 0; (i <= 10000) && (!inputFile.eof()) ; i++)
{
bool check_space =true;
for (int j = 0;((j <= 25) && (! inputFile.eof()) && (check_space)); j++)
{

inputFile.get(a);
if ((a != ' ') && (a != '\o'))
{
words [i][j] = tolower(a);
}
else if ((a == ' ') || (a == '\o'))
{
check_space = false;
}
}
number_words++;
}
cout << words[5];
}

Last edited by jubaitca; 11-03-2006 at 03:55 PM.
 
Old 11-03-2006, 03:59 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Erm, '\o' isn't a valid C escape code, just like the compiler told you. Perhaps you mean '\0'? Also, please wrap code in [code] tags for readiblity.
 
Old 11-03-2006, 04:21 PM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Yes tuxdev is right. The end of an array of characters is a '\0' (and that is a zero, just in case).
But I also see that you are using C++ objets and your file's name ends up with .c. Maybe you should be consistent and work in one language or the other. Plus I believe that if you are coding in C++ you have a string class which is much happier (yet its been a while since I wrote a line of C++ so I cant point much more information on this topic).

Cheers!
 
Old 11-03-2006, 04:32 PM   #4
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tuxdev
Erm, '\o' isn't a valid C escape code, just like the compiler told you. Perhaps you mean '\0'? Also, please wrap code in [code] tags for readiblity.
but is the '\0' a escape character, so that when i check it it means that it is the end of the word? The purpose of my program is to store all the words in the array words[][]. But what happens is that when there is a newline, it takes in two words instead of one.

And also, I am supposed to do this in C++, that's how i was taught to do c++, so this code is intended to be C++.

thanks
 
Old 11-03-2006, 05:10 PM   #5
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
i fixed the problem - instead of using the get function, i used the extraction operator. My next problem is that i have to remove all the punctuation marks at the begenning or end of the word, but not in the middle. How should i do this?

Thanks
 
Old 11-04-2006, 10:12 AM   #6
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
ok....say I am using two char arrays (2 dimensional) how can i store everything from one array to the other except the first index? because strncpy starts from the first index right?
 
Old 11-04-2006, 10:30 AM   #7
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
well this what tried until now to remove the punctuations is its in the first index

Code:
                         else if ((!(isalpha(words[i][0]))) ||
			(!(isdigit(words[i][0]))))
			{
			new_words[i][j] = words[i][j+1];
			}
Although this compiles, its not actually working for me.
 
Old 11-04-2006, 05:59 PM   #8
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Code:
 
else if ((!(isalpha(words[i][0]))) ||
                        (!(isdigit(words[i][0]))))
will always be true

Try this instead to test for alpha or numeric character:
Code:
else if (!isalnum(words[i][0]))
 
Old 11-04-2006, 06:14 PM   #9
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
oh ok thanks. Actually the compiler was right.

But now I have to sort the words. The following is my sorting code, but it gives me a lot of errors for some reason.

If someone could just tell me a general problem for the code, I would really appreciate it. Thanks in advance

Code:
Code:
void SelectionSort(int arr[][MAX_WORD_LENGTH+1], const int size)
{
	int smallest_index;

	for (int i= 0; i <=size; i++)
	{
		smallest_index = i;
		
		for (int j = i + 1; j <= size; j++)
		{
			if (strcmp(arr[j],arr[smallest_index]) < 0)
			{
			 	smallest_index=j;
			}
		
		}
		swap(arr[i],arr[smallest_index]);
	}			
}

void swap_words(char arr[],char arr2[])
{
	char temp[strlen(arr1)];
	strncpy(temp,arr1,strlen(arr1));
	strncpy(arr1,arr2,strlen(arr2));
	strncpy(arr2,temp,strlen(temp));
}
Errors:
test.c: In function `void SelectionSort(int (*)[26], int)':
test.c:67: error: cannot convert `int*' to `const char*' for argument `1' to `in
t strcmp(const char*, const char*)'
test.c: In function `void swap_words(char*, char*)':
test.c:79: error: `arr1' undeclared (first use this function)
test.c:79: error: (Each undeclared identifier is reported only once for each fun
ction it appears in.)
test.c:80: error: `temp' undeclared (first use this function)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h: In function `
void std::swap(_Tp&, _Tp&) [with _Tp = int[26]]':
test.c:73: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:130: error: in
valid initializer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:131: error: IS
O C++ forbids assignment of arrays
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:132: error: IS
O C++ forbids assignment of arrays
 
Old 11-04-2006, 06:34 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
This code is so wrong in so many ways. When you pass arrays around, you must pass their sizes too. Don't use int if you mean char, as in null terminated strings. You 've got a type in the prototype of swap_words. And most of all, you really should be using std::string and std::vector.
 
Old 11-04-2006, 07:08 PM   #11
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
well im not allowed to use vectors yet. I don't understand, how I would pass the sizes, if the size depends on what is being passed. Can you please give an example of where this can be fixed? And also can you tell me which int are you refferring to that is supposed to be char?

Thanks
 
Old 11-04-2006, 07:58 PM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
test.c:67: error: cannot convert `int*' to `const char*' for argument `1' to `in
t strcmp(const char*, const char*)'
This is the compiler telling you that you are trying to put an int* where a const char * should be.

Quote:
test.c:79: error: `arr1' undeclared (first use this function)
This is the compiler telling you that you are trying to use something you haven't told exists (due to a typo).

Your array really should be an array of pointers with dynamically allocated stuff, then just swap pointers, not pointer contents. Copying is slow.
 
Old 11-04-2006, 09:00 PM   #13
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
ok, to tell you the truth, I am really a newbie at C++. I am just learning it at the moment. I haven't learned pointers yet. My teacher said to sort all the words in alphabetical order. My algorithm to do this is to check all the words (which are stored in the array) with the cstring function cstrncmp and then sort it accordingly. To sort it I use the swap function.
 
Old 11-04-2006, 09:06 PM   #14
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Ouch, Ouch Ouch. Do you have a copy of K&R C? It might be really useful reading for you.

Other than the implementation is really bad (sorry it is forced onto you), and definitely the hard way, I don't see any glaringly obvious bugs other than what the compiler told you.
 
Old 11-04-2006, 09:19 PM   #15
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
well, my teacher said I can't use anything that hasn't been taught in class yet. So I am stuck with everything up to cstring. That's about all. I was wondering if you can please give me a hand with my algorithm (my basic approach to do it). Is there something wrong with it?

By the way I am currently studying from the textbook "Problem Solving with C++" by walter savitch.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
true or false? alaios Programming 7 07-16-2005 10:54 AM
warning: comparison is always false due to limited range of data type hubabuba Programming 13 12-21-2004 10:03 AM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM
question about /etc/false notstrider Debian 2 10-23-2004 12:52 AM
False installation p0rtzer0 Linux - Newbie 2 09-23-2004 10:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration