0

I want to ask for word from the user and then convert the word from string to char using 'strcpy'. Then I want to determine the sum of the ascii codes for all of the letters in the word.

However, I am having difficulties. I don't understand exactly how I can do that. This is what I have been able to do so far.

#include <iostream> #include <time.h> #include <stdlib.h> #include <string.h> using namespace std; int main() { string word; cout << "Enter word: "; getline(cin, word); /* char w[word]; strcpy(w,word.c_str()); int ('A'); cout<<char(65); */ return 0; } 

The commented part is where I have been trying to do the converting. I copied the code from a worksheet. Even if it did work, I don't know how, and what it all means.

Thanks for your help.

5
  • 6
    Why are you doing this? Commented Oct 20, 2013 at 23:12
  • You can't make a built-in array with a runtime-determined size. Commented Oct 20, 2013 at 23:13
  • This is my problem prompt: Ask the user for a word (use a textbox). Then determine the sum of the ascii codes for all of the letters in the word and state it in some label. You will need a string.h include and you will need to convert the word (that you get from the user) from string to char using ‘strcpy’ function. Commented Oct 20, 2013 at 23:18
  • 2
    If someone's telling you to use string.h in a C++ program, you need to find a better class. Commented Oct 20, 2013 at 23:24
  • Also, the idea that you need strcpy is pure stupidity. There's simply no need to make a copy, and even if there was, you can copy strings directly. Commented Oct 20, 2013 at 23:26

2 Answers 2

3
char w[word]; strcpy(w, word.c_str()); 

char w[word] is incorrect. The square brackets is for the size, which must be a constant integral expression. word is of type std::string, so this makes neither logical nor practical sense. Maybe you meant it as:

char w = word; 

But that still won't work because word is a string, not a character. The correct code in this case is:

char* w = new char[word.size() + 1]; 

That is, you allocate the memory for w using a char*. Then you use word.size() + 1 to initialize heap-allocated memory amounting to those bytes. Don't forget for the obligatory delete[] when you're finished using w:

delete[] w; 

However, note that using raw pointers and explicit new is not needed in this case. Your code can easily be cleaned up into the following:

#include <numeric> int main () { std::string word; std::getline(std::cin, word); int sum = std::accumulate(word.begin(), word.end(), 0); /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ std::cout << "The sum is: " << sum << std::endl; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Fixed header (it's not in <algorithm>)
@0x499602D2: It's for numerical algorithms, also has std::inner_product, adjacent_difference and partial_sum. C++11 adds iota.
0

You don't need to use strcpy() (or use a char * at all, for that matter), but this'll do your counting using a char pointer:

#include <iostream> #include <string> int main() { std::string word; std::cout << "Enter word: "; std::cin >> word; const char * cword = word.c_str(); int ascii_total = 0; while ( *cword ) { ascii_total += *cword++; } std::cout << "Sum of ASCII values of characters is: "; std::cout << ascii_total << std::endl; return 0; } 

Output:

paul@local:~/src/cpp/scratch$ ./asccount Enter word: ABC Sum of ASCII values of characters is: 198 paul@local:~/src/cpp/scratch$ 

If you really do want to use strcpy(), I'll leave it as an exercise to you to modify the above code.

Here's a better way to do it, just using std::string (and C++11, and obviously presuming your system uses the ASCII character set in the first place):

#include <iostream> #include <string> int main() { std::string word; std::cout << "Enter word: "; std::cin >> word; int ascii_total = 0; for ( auto s : word ) { ascii_total += s; } std::cout << "Sum of ASCII values of characters is: "; std::cout << ascii_total << std::endl; return 0; } 

1 Comment

Thanks for your help. It would be EXTEREMELY helpful if you could do the modification of the code to strcpy(). I really do not know how to do it. Thank you very much anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.