Help with code

Hello C++ community.

I'm currently trying to complete an assignment that has me creating my own functions. I'M NOT ALLOWED TO USE ANY FUNCTIONS AT ALL AND THE ONLY LIBRARY I'M ALLOWED TO USE IS THE <iostream> library.

Currently I thought I was done, but when I entered in the final portion of the project which is to capitalize every letter after a punctuation mark, it didn't work right and would only capitalize the first letter of the new word and fail to do the rest. Next I moved the function call before all the other function calls thinking it could be the order and assigning components of each function that is messing up the other code. When I did this (i'm referencing the ascii table values in my code) it subtracted by 64 ultimately and gave me a parenthesis instead of a capital letter like I want. Could anyone explain/help with me with this code. I'm new and really proud that I made it this far, but I've hit a wall. Thank you!




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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  // no other libraries may be accessed except for namespace
#include <iostream>

using namespace std;

void cap_first(char[]); //caps first letter of word only
void cap_front(char[]); // caps first letters of all words after a space
void cap_all(char[]); // caps all letters of the word
void cap_end(char[]); // caps only after periods, exclamations, question marks

int main()

{
    //local variables only
    char user_word[50];

//    char user_word1 = user_word;

    cout << "Enter your word (Up to 50 characters) " << endl;
    cin.get(user_word, 50, '\n');
    cin.ignore(100, '\n');

    cout << "This is your word: " << user_word << endl;

    cap_end(user_word);

    cap_first(user_word);

    cout << "Your word with only the first letter capitalized is: " << user_word << endl;

    cap_front(user_word); // function call to cap every first letter in word

    cap_all(user_word); // function call to cap every letter in word 


   // cap_end(user_word); // function call to cap every letter after a punctuation
  return 0;
}

void cap_first (char uppercase[])  // changes first letter upper case
{

  if (uppercase[0] >= 'a' &&  uppercase[0] <= 'z')
    {
       uppercase[0] -= 32;
    }
}

void cap_front(char firstUpper[])
{
 //  int len = strupper[];
   cout << "Pass 1" << endl;

  if (firstUpper[0] >= 'A' && firstUpper[0] <= 'Z')
    {
      cout << "You've entered an alphabetical value." << endl;
        int len = 50;
        for( int i = 1; i < len; ++i) // capitalizes ever letter after a space 
          {
             if(firstUpper[i] == ' ')
               firstUpper[i+1] -= 32;
          }
      cout << "Your word with every first letter after a space capitalized is: " << firstUpper << endl;

    }
    else
      cout << "You've entered an incorrect value." << endl;
}

void cap_all (char allupper[])
{
   if(allupper[0] >= 'A' && allupper[0] <= 'Z')
     {
       cout << "You're about to capitalize every letter in this sentence." << endl;
       int len = 50;
       cout << "Pass 2" << endl;
       for(int i = 1; i < len; ++i)
         {
           if(allupper[i] >= 'a' && allupper[i] <= 'z')
             allupper[i]-= 32;
         }
       cout << "Every letter capitalized looks like this: " << allupper << endl;
     }
   else
     cout << "Unable to capitalize everything, sorry. " << endl;
}

void cap_end(char aftercap[])
{
  if(aftercap[0] >= 'A' && aftercap[0] <= 'Z')
    {
      cout << "Your about to capitalize after every punctution mark." << endl;
      int len = 50;
      cout << "Pass 3" << endl;
      for(int i = 1; i < len; ++i)
        {
           if(aftercap[i] == '.' && aftercap[i+1] == ' ' && aftercap[i+2] >= 'a' && aftercap[i+2] <= 'z')
             aftercap[i+2] -= 32;
          else if(aftercap[i] == '?' && aftercap[i+1] == ' ' && aftercap[i+2] >= 'a' && aftercap[i+2] <= 'z')
              aftercap[i+2] -= 32;

           else if(aftercap[i+2] == '!' && aftercap[i+1] == ' ' && aftercap[i+2] >= 'a' && aftercap[i+2] <= 'z')
              aftercap[i+2] -= 32;

        }
      cout << "Your word with capitals after the punctuation marks is: " << aftercap << endl;
    }
  else
     cout << "There were no punctation marks in your word(s). " << endl;
}

                                                      
                                                                                                                
1
2
3
4
5
6
7
8
if(lower >= 'a' && lower <= 'z')
{
	upper = ('A' + lower - 'a');
}
else
{
	upper = lower;
}
Something like that should change a lower case character to an uppercase one.
Last edited on
Topic archived. No new replies allowed.