Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

c++ helpo

  • 11-02-2008 6:05pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭


    Trying to learn this. The program is supposed to take in countries from a text file and arrang them in alphabetical order. get a few errors as of yet:
    #include<fstream> 
    #include<iostream> 
    #include<cstring> 
    #include<string> 
    using namespace std; 
    // swap two strings
    
    void sswap( char **str1, char **str2 ) 
    {
    
    
    	char *tmp = *str1; 
    	*str1 = *str2;
    	*str2 = tmp;
    	tmp = NULL;
    
    }
    
    // ordering strings alphabetically in ascendant or descendant order
    
    void alphasort( char str[1000], const int &strnum, bool ascendant ) 
    {
    
    	int result = 0; 
    
    	for( int i = strnum - 1; i > 0; i-- ) 
    		{
    	for( int j = 0; j < i; j++ ) 
    		{
    
    			[B]result = ( str[j].compare(str[j+1]) );[/B]
    
    	if(( ascendant && result > 0 ) || ( !ascendant && result < 0 )) 
    	{
    
    		[B]sswap( &str[j], &str[j+1] );[/B]
    	}
    
    		}
    
    		}
    
    }
    
    // displays a list countries on the screen
    
    void displayCountries( char **Country, int country_number ) 
    {
    
    
    	for( int k = 0; k < country_number; k++ ) 
    {
    
    cout << "(" << k+1 << ")" << Country[k]; 
    
    if( k != country_number - 1 ) 
    	{ 
    		cout << endl;
    
    	}
    
    }
    
    }
    
    
    void main() 
    {
    
    	const int number = 10; // the number of countries 
    
    	char Country[1000]; 
    	ifstream a_file("U:/test.txt" ); 
    	a_file>> Country;
    	cout<< Country;
    
    
    cout << "\nList of countries before alphabetical ordering:" << endl; 
    [B]displayCountries( Country, number );[/B]
    
    
    // perform ascendant alphabetical ordering 
    alphasort( Country, number, 1 );
    
    cout << "\n\n\n" << "after ascendant alphabetical ordering:" << endl; 
    [B]displayCountries( Country, number );[/B]
    
    cout << endl;
    
    }
    

    The errors I get are:
    1>Compiling...
    1>AssemblyInfo.cpp
    1>yeah.cpp
    1>.\yeah.cpp(31) : error C2039: 'compare' : is not a member of 'System::SByte'
    1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::SByte'
    1>.\yeah.cpp(37) : error C2664: 'sswap' : cannot convert parameter 1 from 'char *' to 'char **'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>.\yeah.cpp(82) : error C2664: 'displayCountries' : cannot convert parameter 1 from 'char [1000]' to 'char **'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>.\yeah.cpp(89) : error C2664: 'displayCountries' : cannot convert parameter 1 from 'char [1000]' to 'char **'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>Generating Code...
    1>Build log was saved at "file://u:\a\yeah\yeah\Debug\BuildLog.htm"
    1>yeah - 4 error(s), 0 warning(s)
    Not sure how to fix em. The lines with errors are in bold.


Comments

  • Registered Users, Registered Users 2 Posts: 1,322 ✭✭✭Mad_Max


    Not a c++ guy by anymeans but i'd say based on your error output that your problem is string -vs- chars.

    You have declared your array holding the countries as Char but are using the compare method which is a string method. Try using a numeric operator such as < and that should work.

    Im not familiar with the swap method but i'm going to have a guess and say its most likely a string problem again. Im sure someone else will inform you better on this.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    Thanks, I know that I am having a problem with this string vs char thing, I just am not good at fixing it.
    :-)

    Also anybody know what char ** means?
    One * is a pointer, what is two?


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Thanks, I know that I am having a problem with this string vs char thing, I just am not good at fixing it.
    :-)

    Also anybody know what char ** means?
    One * is a pointer, what is two?

    Also a pointer :D

    char ch = 'H'; // ch is the value 'H'
    char *pc = &p; // pc is a pointer to char and points to ch
    char **ppc = &pc; // ppc is a pointer to a pointer to char and points to pc.

    Yeah. In effect char ** means an "array of pointers to char" or an "array of strings".

    Conor.


  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    Can you not compare the chars directly seeing that they hold a numeric value...

    signed char 1 8 -128 -> +127
    unsigned char 1 8 0 -> +255

    It has been a while since I have used C/C++

    But I think a direct comparison like
    if(c[j]>c[j-1]){
    sswap(&c[j],&c[j-1])
    }
    

    Obviously I'll be corrected if i'm wrong... you may need to convert them to the int value first or something but you get the general jist. Damn these higher level languages ruining my brain.


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    Ok first of all you're using char** which as you know is essentially a pointer to an array of strings (really a pointer to a pointer to type char). Once you go over one level of indirection (*)s things can get very messy.

    Personally I'd use a struct to get rid of the char**.
    struct data
    {
       char m_country[50];
    };
    
    //....
    
    // use a reference
    swap(struct data*& p, const int num)
    {
       // do your comparison and just a matter of switching elements
       
       // How you do your comparison is up to you, but it's very simple with a two loops
    }
    
    
    display(struct data* p, const int num)
    {
    
    }
    
    void main(..)
    {
       // ask user how many countries
    
       // allocate that amount
       data *p_buffer = new data[number];
    
       // read in data
    
       display(p_buffer, number);
       
       compare(p_buffer, number);
    
       delete [] p_buffer;
    }
    

    Are you not allowed use pre-written string comparison functions like strcmp() and std::string::compare ? If you can I'd suggest using them.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    I don't wish to be brutal but my, what a mess! You'll need to have someone sit down and talk you through it. You've patched together bits of code into a confusion between pointers, arrays, char and strings. I don't know where to start!

    I'm afraid I don't do C++ and don't get the ifstream template business but I'm pretty sure that:

    a_file>> Country;

    will read one line from your file (that at least looks fine). I believe a little snippet would go along the lines of:

    while ( a_file >> Country )
    cout << Country << endl;

    Which is a good place to start. Ie: start with just those lines and see can you get something working! Bear in mind that Country as declared in your code is enough space for 1000 characters (ie: a single string).

    Then see can you read 10 countries into an array:

    char countries[10][50] -> 10 items of 50 chars each.

    and use countries to select which country (0 to 9) you want. Build up piece by piece and you'll gain some understanding...

    One thing to bear in mind - an array and a pointer behave the same but are not the same thing - in other words don't go treating the above countries array as a set of pointers to strings.

    You've a big hill in front of you. One step at a time :D

    Conor.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yes bit of a mess alright!

    Being a while now since i've done C++ but few things:

    Opening File for reading:
    //get file
    ifstream myinfile;
    myinfile.open("inputfile.txt");
    
    if (myinfile.fail()) 
    {
      cerr << "Error opening data file\n";
      exit(1);
    }
    

    A way of reading file line by line
    int const CAPACITY = 10;
    string countries[CAPACITY];
    string line;
    
    for(int k=0; k<CAPACITY; k++)
    {
       while(getline(myinfile, line))
          counties[k] = line;
    	
       if (myinfile.eof()) 
          break;
    }
    

    Now you have a string array of capacities.
    You can sort from there then - there is a useful function for sorting vectors in C++ that I'm not too familiar with but may be useful.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    Thanks guys, I got this program working.


Advertisement