Jump to content

(C++) No errors, but program wont run the way it should!

TallOne123
Go to solution Solved by TallOne123,

Well the primary problem seems to be your size value. I don't really see you updating it and you really shouldn't have it anyway. The size of the array can be obtained easily. Either update the size, or stop relying on that value (which would be the better option since you can get the length of any arbitrary array myArray with myArray.size() ).

 

Debugging tips: Print out every variable that you think may be causing the problem. You figured that at one point it didn't work, which is good, but printing out can help you determine the culprit. Perhaps, use standard error to print them out since you aren't using it in this case to make it easier to clean up.

Hey, thanks for the reply. I ended up fixing my problem but I don't want to get into too much detail since I had to play with my header file as well.

 

You guys rock.

Good evening LTT. I come asking about my homework assignment  because frankly, I am completely STUMPED.

 

This is pretty much how it should go - When you're asked for an input of sentences, the program will count the characters.

 

Ex. Input: My name is TallOne123

      Output: You wrote: My name is TallOne123

                   M = 1, n = 2, y = 1, m = 1, a = 2, e = 2, i = 1, s = 1, T = 1, l = 2, O = 1, 1 = 1, 2 = 1, 3 = 1

 

The strange thing is, my compiler shows no error messages at all! Here's the piece of code

#include <iostream>using namespace std; const int CHAR = 132; int Input(char sentenceString[CHAR]);void CountandDisplay(int size, char sentenceString[CHAR]); int main(){char sentenceString[CHAR];char loop = 'y';char prompt;int count = 0; do{cout << "Menu" << endl;cout << "\n(E)nter the sentence to process: " << endl;cout << "\n(C)ount the letters and then display them" << endl;cout << "\n(Q)uit the program." << endl;cout << endl;cin >> prompt;cin.ignore(); system("cls"); switch (prompt){case 'c':case 'C':CountandDisplay(count,sentenceString);system("pause");system("cls");break; case 'e':case 'E':Input (sentenceString);system("cls");break; case 'q':case 'Q':loop = 'N';break; }} while((loop == 'y' || loop== 'Y'));} int Input(char sentenceString[CHAR]){int size = 0; cout << "Input a sentence of up to 132 characters: ";cin.getline(sentenceString, CHAR);size = cin.gcount(); // count all characters using cin.gcountreturn size; system("pause");} void CountandDisplay(int size, char sentenceString[CHAR]){short CharCountBuf[127];int charval = 0;int *InBuf = 0;char character = ' ';int count = 0; cout << "Your sentence is: " << sentenceString << endl; for (int i=0; i<127; i++)CharCountBuf[i] = 0; for(int i=0; i<size; i++){      if ( (sentenceString[i] >= 'a') && (sentenceString[i] <= 'z') ) //lower case      {          charval = (int) sentenceString[i];          CharCountBuf[0+charval] += 1;  // Increase char count }  else if ((sentenceString[i] >= 'A') && (sentenceString[i] <= 'Z')) //if it's uppercase{charval = (int)sentenceString[i]; CharCountBuf[charval]+1;} else if ((sentenceString[i] >= '0') && (sentenceString[i] <= '9')) // and numbers{charval = (int)sentenceString[i]; CharCountBuf[charval]++; }}  for (int i = 0; i < 127; i++){if (CharCountBuf[i] > 0){character = (char)i;cout << character << ":  " << CharCountBuf[i] << endl;}}}

 

In my program, I only get as far as outputting just the sentence. It doenst count the characters at all

Link to comment
Share on other sites

Link to post
Share on other sites

It's great of you to use spoiler tags for your code, but please also use code tags and indent your code properly to make it easier to read.

[code]#include <iostream>using namespace std; const int CHAR = 132; int Input(char sentenceString[CHAR]);void CountandDisplay(int size, char sentenceString[CHAR]); int main(){    char sentenceString[CHAR];    char loop = 'y';    char prompt;    int count = 0;     do    {        cout << "Menu" << endl;        cout << "\n(E)nter the sentence to process: " << endl;        cout << "\n©ount the letters and then display them" << endl;        // ...[/code]
Link to comment
Share on other sites

Link to post
Share on other sites

 

Good evening LTT. I come asking about my homework assignment  because frankly, I am completely STUMPED.

 

This is pretty much how it should go - When you're asked for an input of sentences, the program will count the characters.

 

Ex. Input: My name is TallOne123

      Output: You wrote: My name is TallOne123

                   M = 1, n = 2, y = 1, m = 1, a = 2, e = 2, i = 1, s = 1, T = 1, l = 2, O = 1, 1 = 1, 2 = 1, 3 = 1

snip

I'm confused is your example just an example of what should happen? Or is it the actual output but something else should happen?

|CPU: Intel i7-5960X @ 4.4ghz|MoBo: Asus Rampage V|RAM: 64GB Corsair Dominator Platinum|GPU:2-way SLI Gigabyte G1 Gaming GTX 980's|SSD:512GB Samsung 850 pro|HDD: 2TB WD Black|PSU: Corsair AX1200i|COOLING: NZXT Kraken x61|SOUNDCARD: Creative SBX ZxR|  ^_^  Planned Bedroom Build: Red Phantom [quadro is stuck in customs, still trying to find a cheaper way to buy a highend xeon]

Link to comment
Share on other sites

Link to post
Share on other sites

I'm confused is your example just an example of what should happen? Or is it the actual output but something else should happen?

It's an example of how it SHOULD be ran. It asks for a sentence input, then outputs the sentence as well as how many uppercase and lowercase letters, as well as numbers were used.

In my program, I only get as far as outputting just the sentence. It doenst count the characters at all

Link to comment
Share on other sites

Link to post
Share on other sites

Well the primary problem seems to be your size value. I don't really see you updating it and you really shouldn't have it anyway. The size of the array can be obtained easily. Either update the size, or stop relying on that value (which would be the better option since you can get the length of any arbitrary array myArray with myArray.size() ).

 

Debugging tips: Print out every variable that you think may be causing the problem. You figured that at one point it didn't work, which is good, but printing out can help you determine the culprit. Perhaps, use standard error to print them out since you aren't using it in this case to make it easier to clean up.

Link to comment
Share on other sites

Link to post
Share on other sites

Well the primary problem seems to be your size value. I don't really see you updating it and you really shouldn't have it anyway. The size of the array can be obtained easily. Either update the size, or stop relying on that value (which would be the better option since you can get the length of any arbitrary array myArray with myArray.size() ).

 

Debugging tips: Print out every variable that you think may be causing the problem. You figured that at one point it didn't work, which is good, but printing out can help you determine the culprit. Perhaps, use standard error to print them out since you aren't using it in this case to make it easier to clean up.

Hey, thanks for the reply. I ended up fixing my problem but I don't want to get into too much detail since I had to play with my header file as well.

 

You guys rock.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×