Need Help With Char Array Index Conversion

I am working on a program that prompts the user to enter a sentence and translates the characters in the sentence to their corresponding integers in an array. Then prints the sum of the integers in each word. Can someone take to see if I am doing this program correctly. Im not sure if Im converting the characters to integers correctly (lines 21-25) and converting the sentence inputted in by the user to all lower case characters (lines 26-34). PLEASE HELP!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// This program prompts the user to enter  a sentence and translates the characters
// into their corresponding integers in an array. Then prints the sum of the 
// integers in each word.

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

using namespace std;

int main()
{
	char Alpha[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
	char str[]= "The big Cat.\n";
	char sentence;
	int count, value;
	// letters to numbers
	for (count = 0; count < 26; count++)
	{
		value = count;
	}
	// convert sentence to lower case
	cout << "Enter your sentence: ";
	cin >> sentence;
	while (str[count])
	{
		sentence = str[count];
		putchar(tolower(sentence));
		count++;
	}


	return 0;
}
Have you tried stepping through your program pretending that you are the computer following the instructions that you have been given as code?
Lines 19-22 : The for loop starts count at 0, then executes the assignment on line 21. value now is 0, count is incremented by 1, count is not less than 26, so the loop goes again. Blah blah blah. At the end of the loop value is 25 and count is 26. Did you mean for that whole for loop to do nothing more than this?
int count = 26, value = 25;
Try walking through your program following all the instructions to see if these are really the instructions you meant.
As kevinjt2000 pointed out, the loop from 19-22 does nothing other than to assign 25 to value. Did you intend value to be an array?

line 25: This won't read a sentence. cin stops at the first whitespace. You want to use getline() instead.

line 16: sentence is a single character. It's pretty hard to fit an entire sentence into a single character.

liner 28: You're making an out of bounds reference. count was left at 26 when you exited the for loop at line 22. str[26] is out of bounds. Did you mean to reset count to 0 before entering the while loop?

I don't see any calculation of a sum of the integers.

Last edited on
Topic archived. No new replies allowed.