Close

Results 1 to 13 of 13

Thread: C++

  1. #1
    DF VIP Member easy's Avatar
    Join Date
    Aug 2003
    Location
    scotland, UK
    Posts
    1,028
    Thanks
    5
    Thanked:        0
    Karma Level
    304

    Default C++

    I have to make a program in a win32 app so that when i enter a first and last name, it displays them, the ascii decimal equivalent for every character then adds the decimal for every character to give a total value.

    Where do i start? I have so far, written a program where a name can be entered and displayed but the bit that i'm totally stuck on is how do i turn the charactors into a decimal equivalent. I am a begginner at C++, so simplicity would be advised. I am using MS visual studio 2k5.

    Cheers

  2. #2
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: C++

    Does it have to be a win32 app or can it be a console app?

    [edit]

    Ooops, just realised you've done the app and just need to add the chars!
    I'm not into C++ really, I prefer C so here is your app in C which you can prehaps get some pointers from (pun kind of intended. ).

    John

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(int argc, char **argv) {
    	char *my_string="Hello Moto!";
    	unsigned int count=0;
    	unsigned int total=0;
    
    	while (count<strlen(my_string)) {
    		printf("Adding character '%c' (%i)\n",*(my_string+count),*(my_string+count));
    		total+=*(my_string+count);
    		count++;
    	}
    
    	printf("\nTotal value of this string = %i\n",total);
    	
    	return 0;
    }
    [further edit]

    Attached a compiled version for you.
    Last edited by /dev/null; 16th March 2007 at 02:47 PM.

  3. #3
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    389

    Default Re: C++

    Or use this version:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv) {
        char *my_string="Hello Moto!";
        unsigned int total=0;
        const unsigned char* current = (unsigned char*)my_string;
    
        while( *current )
        {
            printf("Adding character '%c' (%i)\n",*current,*current);
            total+=*current++;
        }
    
        printf("\nTotal value of this string = %i\n",total);
    
        return 0;
    }

    This will save the strlen, the count variable as well as (possibly) multiple array references. Also it will work for 8-bit character sets.

    You could even impress them further by putting in a caveat at the end of your documentation explaining that the application is fine for 8-bit but in this day and age internationalization (e.g. Unicode/MultiByte) should really be considered

  4. #4
    DF VIP Member easy's Avatar
    Join Date
    Aug 2003
    Location
    scotland, UK
    Posts
    1,028
    Thanks
    5
    Thanked:        0
    Karma Level
    304

    Default Re: C++

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char FirstName[16];
    	char LastName[16];
    
    	cout << "Enter your First Name please...""\n";
    
    	cin >> FirstName;
    
    	cout << "Now will you please enter your Last Name...""\n";
    
    	cin >> LastName;
    
    	cout << "Thank You""\n";
    
    	cout << "\nYour Full Name is: " << FirstName << " " << LastName << "\n\n";
    	return 0;
    }
    Any improvements on the above? How do i combine the two? Thank you so far!

  5. #5
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: C++

    Well, you have the names stored as char arrays, so just plug the code in that loops through those from the post(s) above and you should be ok! Also, seeing as both the first name and last name are both of length 16, you can do it with just 1 loop.
    The only differences to note are that printf is used above as opposed to cout however that ain't a problem as long as you print the variables correctly. You will (I would imagine) have to cast the char to an int in order to print out the ascii number using cout.

  6. #6
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    389

    Default Re: C++

    John, did u get VS2005 sorted in the end?

  7. #7
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: C++

    Oh ye, forgot about that, lol - I installed Visual Studio 6.0 which seems to work ok.... Unless there are any major differences then it'll do for now.

  8. #8
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    389

    Default Re: C++

    The IDE is infinately better except for their new MFC wizard

    As I said if u want it you know where to ask

  9. #9
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: C++

    lol. I should be ok then with v6.0 as I hate using an "IDE" (whatever that may be. ) anyway, lol!

    Cheers though - I'l let ya know if it changes!

  10. #10
    DF VIP Member easy's Avatar
    Join Date
    Aug 2003
    Location
    scotland, UK
    Posts
    1,028
    Thanks
    5
    Thanked:        0
    Karma Level
    304

    Default Re: C++

    Ok so i havent really got any further but i am gradually understanding everything abit more which is generally the aim of this, so thank you. I figure that all i have to do really to get the entire program up and running so i have atleast something complete is to take the two name's being input and insert them into the 2nd half of the program where 'helo moto!' is. I know i have the inputs stored so how do i loop them into the 2nd half as directed above? (in Layman's terms please)

  11. #11
    DF VIP Member easy's Avatar
    Join Date
    Aug 2003
    Location
    scotland, UK
    Posts
    1,028
    Thanks
    5
    Thanked:        0
    Karma Level
    304

    Default Re: C++

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    int main(int argc, char **argv) 
    {
    	char Firstname[16];
    	char Lastname[16];
    		
    		printf("enter your first name\n");
    		
    		cin >> Firstname;
    
    		printf("enter your last name\n");
    
    		cin >> Lastname;
    
    
        char *my_string= "Hello moto!";
    
        unsigned int total=0;
    
        const unsigned char* current = (unsigned char*)my_string;
    
        while( *current )
        {
            printf("Adding character '%c' (%i)\n",*current,*current);
            total+=*current++;
        }
    
        printf("\nTotal value of this string = %i\n",total);
    
        return 0;
    }
    So how do i bridge this gap then? ie. Take char Lastname & char Firstname and input them into my_string. I have to have this complete for Tuesday so time is starting to become a factor here. Thank you.

  12. #12
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    389

    Default Re: C++

    Personally I don't think we should be doing all your homework or else it makes a farce of the education system as you never learn.

  13. #13
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: C++

    ^^As said above, you should try to do some of this yourself... All you need do is switch a couple of variables around, maybe put in a couple of lines yourself and it'll be done! Give it a try, lol!

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •