| | |
How How How???!!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 0
you could try implementing your own version of strcmp?
strcmp() maintains two pointers to char, one for the first string, one for the second - it compares the values of what the pointers point to (ie the value of the char currently being pointed to) - if this value is zero (i.e. equal) then it increments the pointers and compares the next pair of bytes.
otherwise it returns with either a positive or negative value.
as far as I know strcmp() will return positive or negative based only on the first pair of bytes.
does that help?
strcmp() maintains two pointers to char, one for the first string, one for the second - it compares the values of what the pointers point to (ie the value of the char currently being pointed to) - if this value is zero (i.e. equal) then it increments the pointers and compares the next pair of bytes.
otherwise it returns with either a positive or negative value.
as far as I know strcmp() will return positive or negative based only on the first pair of bytes.
does that help?
http://www.daniweb.com/techtalkforum...cement8-2.html
Oh, and here for how to write a post in the first place.
http://www.catb.org/~esr/faqs/smart-...tml#bespecific
Oh, and here for how to write a post in the first place.
http://www.catb.org/~esr/faqs/smart-...tml#bespecific
•
•
Join Date: Apr 2007
Posts: 40
Reputation:
Solved Threads: 0
[qoute]SIr thnk you for a wonderful advice..
Ahmm.. briefly how will i operate the program correctly if the output of my program is always at the first line only of my database...
example... my problem is i will type a country code:
OUTPUT:
Enter Country Code : US (e.g.)
/* the answer will be like this.. */
United States: Digicel, Movistar and other telephone companies in that country,,
MY OUTPUT:
Enter Country Code: US(e.g.)
Afghanistan //this is always the reply of my input..

(at my database)
hmmm. it is something like this...
AF - Afghanistan
AX- Aland islands
......
PH - Philippines: Cure, Globe, Smart, Sun
US - United States: Digicel, Movistar etc...

/*my program is like this..
Hope u can help me..*/

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char code[5];
char whole[800];
cout << "\n\n\tType the Country Code: \t\t ";
cin >> code;
FILE *f;
f=fopen("countrycode.csv", "r");
if(f==NULL) cout<< "cant open";
else
{
while(fgets(whole,800,f)!=0)
{
int a=0, i;
char delimeter[]="-\"", who[800][800];
char *huj=strtok(whole, delimeter);
while(huj!=NULL)
{
strcpy(who[a],huj);
if(a++>=sizeof who/ sizeof *who)
{
break;
}
huj=strtok(NULL, delimeter);
}
for (i=0; i<a; i++)
{
if(strcmp(code,who[1])!=0)
{
strcmp(code,who[1]);
cout << who[1]<<endl;
exit(1);
}
else
{ cout << "INVALID!!!";
exit(1);
}
} }
fclose(f);
}
return 0;
}
Ahmm.. briefly how will i operate the program correctly if the output of my program is always at the first line only of my database...
example... my problem is i will type a country code:
OUTPUT:
Enter Country Code : US (e.g.)
/* the answer will be like this.. */
United States: Digicel, Movistar and other telephone companies in that country,,
MY OUTPUT:
Enter Country Code: US(e.g.)
Afghanistan //this is always the reply of my input..

(at my database)
hmmm. it is something like this...
AF - Afghanistan
AX- Aland islands
......
PH - Philippines: Cure, Globe, Smart, Sun
US - United States: Digicel, Movistar etc...

/*my program is like this..
Hope u can help me..*/


#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char code[5];
char whole[800];
cout << "\n\n\tType the Country Code: \t\t ";
cin >> code;
FILE *f;
f=fopen("countrycode.csv", "r");
if(f==NULL) cout<< "cant open";
else
{
while(fgets(whole,800,f)!=0)
{
int a=0, i;
char delimeter[]="-\"", who[800][800];
char *huj=strtok(whole, delimeter);
while(huj!=NULL)
{
strcpy(who[a],huj);
if(a++>=sizeof who/ sizeof *who)
{
break;
}
huj=strtok(NULL, delimeter);
}
for (i=0; i<a; i++)
{
if(strcmp(code,who[1])!=0)
{
strcmp(code,who[1]);
cout << who[1]<<endl;
exit(1);
}
else
{ cout << "INVALID!!!";
exit(1);
}
} }
fclose(f);
}
return 0;
}
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 0
It looks like in the loop:
might be your problem - why are you only comparing code with who[1]. If you do that, then yes you will always get "Afghanistan" as your output...
Should who[1] be who[i] perhaps?
Can you explain a bit more what it is your attempting to do?
best wishes
wise_monkey
C++ Syntax (Toggle Plain Text)
for( i=0; i<a; i++) { if(strcmp(code, who[1])!= 0) { strcmp(code, who[1]); cout << who[1] << endl; exit(1); } }
might be your problem - why are you only comparing code with who[1]. If you do that, then yes you will always get "Afghanistan" as your output...
Should who[1] be who[i] perhaps?
Can you explain a bit more what it is your attempting to do?
best wishes
wise_monkey
![]() |
Other Threads in the C++ Forum
- Previous Thread: plz! trubleshoot my program
- Next Thread: LNK2001 error
Views: 958 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







