Ahmmm .. I'm a newbie in C++ programming, and my BOSS wants me to do a program that accepts a COUNTRY CODE and its output must be the COUNTRY NAME of that COUNTRY CODE and following on the COUNTRY NAME is there Biggest TELEPHONE OPERATOR..:?:

******************************************************************
OUTPUT:

Please Enter Country Code: US (e.g.)

United States: Digicel, US Mobil etc...

*****************************************************************
I have a database save in countrycode.csv that has the all list of the country code with their country names and their telephone operators,,,
My problem is the output is always segmentation fault..
MR.DANI or guys/ladies(proffesionals) please give me some ideas how to solve it... thanks :confused::rolleyes::sad::-|
My examined main source program is something like this..

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

using namespace std;
int main()
{
char code[10];
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[100][100];
            
            

        char *huj=strtok(whole, delimeter);
        while(huj!=NULL)
    {
            
        if(a++>=sizeof who/ sizeof *who)
        {
            break;
        }
            huj=strtok(NULL, delimeter);
        }
        

    for (i=0; i<a; i++)
    {
    strncpy(who[i],huj,300);
    if(strcmp(code,who[0])!=0)
    {
        cout << who[1]<<endl;
        exit(1);
    }
    else
    {    cout << "INVALID!!!";
            exit(1);
      }
    }
    }
fclose(f);
    }
return 0;
}

:rolleyes:

> MR.DANI
:lol:

As for your code, why in the world are you mixing C and C++? If you're going to use C++, make sure you use ifstream for your file input.

>> strncpy(who,huj,300);
This line is probably causing the problem. each element of who can only contain 100 characters and that line is copying up to 300 characters into it. Won't fit.

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.