,DIV,TABLE,THEAD,TBODY,TFOOT,TR,TH,TD,P { font-family:"Nimbus Sans L"; font-size:x-small } -->
ahmm hi everyone i'm a newbie here at daniweb.. but i have an unfinished business(project).. guys.. pls.. help me to solve this.. or give some comments or replies in my problem..

this is my problem.. i don't know what function/s should i use to compare all the chars on all the lines of my database.. ahmmm.. i want the program output will be like this..


EXPECTED OUTPUT:


Enter the Country Code: AF(ex.) // in my program if you type US afghanistan is always a reply statement..

Afghanistan: Movistar, Digicel (ex. telephone operators)

THIS IS MY CODE (hope u help me to solve my problem..:

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

using namespace std;
int main()
{
    char code[3];
    char whole[600];
    char who[300][300];
    cout << "\n\n\tType the Country Code: \t\t ";
    cin >> code;
    FILE *f;    
    f=fopen("countrycode.csv", "r");
    if(f==0) cout<< "SORRY can't find or open the file";
    else
    {
        while(fgets(whole,900,f)!=NULL)
        {
//char *u=strstr (whole, "-");
            int a=0, i;
//if (u==0)
//char delimeter[]="-\"";
//char who[300][300];
            {
                char *huj=strtok(whole, "-\"");
                while(huj!=NULL)
                {
                    strcpy(who[a],huj);            
                    if(++a >= sizeof who/ sizeof *who)break;
                    huj=strtok(NULL, "-\"");
                }
            //for (i=0; i<a; i++)
                //int i, b=EOF;    
                //(i=0);
                //while (i<b);    
                //while (fgets(whole, 900,f)!=NULL)
                {    
                    
                    if(strcmp(who[i],code)==0)
                    {
                        strcmp(who[0],code);
                        cout /*<< "if:"*/<<who[1]<<endl;
                        //exit(1);    
                    }
                    else 
                    {    strcmp(who[0],code);
                        cout /*<<"else:"*/<<who[1];//<< " "<<code<<endl;
                        //cout << "INVALID!!!";
                        //exit(1);
                     }
                //(i++);
                    
                }    
            }
        //cout << "while  loop: " <<whole << endl;

        }
fclose(f);
    }
return 0;
}

*******************************************************************

and that is my program code.. the database is save as .csv file format.. ahmm please give me some advice..

At my database is look like this

AF - Afghanistan: (telephone operators)
AX - Aland Island: (not yet found its telephone operator)
....
US - United States: Digicel, Movistar, Us Phone Company...etc..

*******************************************************************

and this is my program hope that i finish this project as soon as possible.. thnks.. God Bless!

Dani AI

Generated

— the root cause is the parsing and indexing approach. Using fixed C buffers, strtok and manual index math makes it easy to overrun buffers, use an uninitialized index, or print the wrong token. is right that this has been discussed before; use a simple, robust C++ approach: read each line with std::getline, split at the delimiter (for example -), trim whitespace and carriage returns, normalize the country code (uppercase), and store lines in a map for fast, correct lookup.

Common problems to avoid:

  • Buffer overflows from mismatched sizes and using fgets with a larger length than the array.
  • strtok modifies the input and can be confusing when delimiters vary.
  • Not trimming trailing \r from Windows files or a UTF-8 BOM at the start of the file.
  • Comparing raw char arrays without normalizing case or trimming spaces, which yields false negatives.

Recommended pattern (concise example):

// load file into an unordered_map keyed by normalized country code
// read file line-by-line, find '-' delimiter, trim parts, uppercase the code
// then prompt for code, normalize input and lookup in the map
// (see full snippet in the post for exact implementation)

Troubleshooting tips: confirm the CSV encoding (remove BOM if present), verify the delimiter used in every line, test with a tiny file first, and add diagnostics that print each parsed key while loading. Switching to std::string/streams and a container like std::unordered_map will both simplify the code and eliminate the off-by-one / uninitialized-index bugs that cause the wrong country to be returned.

we have already gone over this problem here.

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.