C++ Strings

Running into a problem with strings. My code will only work if the array i have is intialized..

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
#include <string>
#include <iostream>

int main()
{
    using namespace std;
    
    const int ArrayIndex = 6;
    char MyArray[ArrayIndex] = "ABCDE";  
      
/*    
    string usrStr;
    cout << "Enter a string: ";
    getline ( cin, usrStr );
    cout << "You Entered: " << usrStr << endl << endl;
*/         
       
    
    //using a ptr to search thru each element of the array
    for ( char *CharPtr = MyArray; CharPtr < MyArray + ArrayIndex - 1; CharPtr++ )
    {
        switch ( *CharPtr )
        {
               case'A':
               case'a':
                       cout << "A is a Vowel" << endl;
               break;
               case'e':
               case'E':
                       cout << "E is a Vowel" << endl;
               break;
               case'I':
               case'i':
                       cout << "I is a Vowel" << endl;
               break;
               case'o':
               case'O':
                       cout << "O is a Vowel" << endl;
               break;
               case'U':
               case'u':
                       cout << "U is a Vowel" << endl;
               break;
               default:
                       cout << "Non-Vowel Detected.." << endl;
                       
        }                                     
    }     
    
    system("pause");
    return 0;
}


The code will work just fine. But I need it to work with a string that the user will type..
Last edited on
Try with the string subscripting:
1
2
string str = "abcdefg";
str[1] == 'b';
Last edited on
Do you mean individually putting each letter in a seperate array?
..
Could u be a little more detailed ?

The commented portion of my original post is what i want to use get user input. Then use that varaible as the switch condition. But the switch 'quantity' is not of type integer.

But is this what u mean?
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
int main()
{
    string ch;
    int i;
    char MyArray[6] = "";
    
    cout << "Enter a string: ";
    getline ( cin, ch );
    
    
    cout << "You Entered " << ch << endl << endl;
    
    int len = ch.length();
    for(int i=0; i<= ch.length(); i++)
    cout << ch[i];
    
    char usrOption;
    
      switch (  )
      {
           case'o':
                   cout << "O is a vowel " << endl;
           break;
           case'e':
                   cout << "E is a vowel " << endl;
      }        
                 

    
    
    system("pause");
    return 0;
}


the switch conditon :p
(From your firs post)
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
#include <string>
#include <iostream>

int main()
{
    using namespace std;
      
    string usrStr;
    cout << "Enter a string: ";
    getline ( cin, usrStr ); // getting string
    int length = usrStr.length(); // getting length
    cout << "You Entered: " << usrStr << endl << endl;   

    for ( int i = 0; i < length; i++)//iterate between the characters in the string
    {
        switch ( usrStr[i] )//works in a similar way to an array
        {
               case'A':  case'a':              
               case'E':  case'e':            
               case'I':  case'i':             
               case'O':  case'o':               
               case'U':  case'u':
                  cout << toupper(usrStr[i]) << " is a Vowel" << endl;//toupper should be in ctype.h
                  break;
               default:
                   cout << "Non-Vowel Detected.." << endl;                      
        }                                     
    }     
    
    system("pause");//you should avoid this ( http://www.cplusplus.com/forum/beginner/1988/ )
    return 0;
}
Last edited on
rite on the money tytyty
Hey Bazzy,

Was wondering if you could check this out real fast..I think its correct.

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
#include <iostream>
#include <string>   // Required for String usage
#include <limits>   // To eliminate the problems caused by system"(pause)"
#include <cmath>    // Required for Logrithmic Functions

int main()
{
    using namespace std;
      
    string usrStr;
    cout << "Enter a string: ";
    getline ( cin, usrStr ); // getting string
    
    int slength = usrStr.length( ); // getting length
    
    cout << "You Entered: " << usrStr << endl << endl;   
    
    double lgResult = 0;
         
         // iterate through each element of the array
    for ( int usrStrIndex = 0; usrStrIndex < slength; usrStrIndex++ )
    {
        double VowelA,
        VowelE,
        VowelI,           // declare VowelVaraibles for the reletive probabilities
        VowelO,
        VowelU,
        OtherChars,
        ;
        
        switch ( usrStr[usrStrIndex] )
        {
               case'A':
               case'a':
                       cout << "A is a Vowel" << endl;
                       VowelA = - ( 0.03 * log ( 0.03 ) ) ;
                       lgResult = lgResult + VowelA ;    
        // If a or A is entered add log calculation to lgResult
    
               break;      
               case'e':
               case'E':
                       cout << "E is a Vowel" << endl;
                       VowelE = - ( 0.10 * log ( 0.10 ) ) ;
                       lgResult = lgResult + VowelE;
                       
                                                        
               break;
               case'I':
               case'i':
                       cout << "I is a Vowel" << endl;
                       VowelI = - ( 0.04 * log ( 0.04 ) ) ;
                       lgResult = lgResult + VowelI;
                       
                                 
               break;
               case'o':
               case'O':
                       cout << "O is a Vowel" << endl;
                       VowelO =  - ( 0.02 * log ( 0.02 ) ) ;
                       lgResult = lgResult + VowelO;
                       
               break;
               case'U':
               case'u':
                       cout << "U is a Vowel" << endl;
                       VowelU = - ( 0.01 * log ( 0.01 ) ) ;
                       lgResult = lgResult + VowelU;
                                       
               break;        
              default:
                                  
                       cout << "Non-Vowel Deteteced.." << endl;
                       
                       OtherChars = - ( .038 * log ( .038 ) ) ;   
                       lgResult = lgResult + OtherChars;                             
                       
        }              
       
            }     
 // Whatever Possibility the user types will be calculated and displayed
cout << "Logrithmic Sum of Vowels and / or Non-Vowels is: ";
cout << lgResult << endl;
            
cout << "Press Any Key to Continue.. " << endl << endl;
cin.ignore ( numeric_limits <streamsize>::max(), '\n');    

return 0;
}

Last edited on
At line 28 you have an extra comma
cmath should be the C header, C++ uses math.h
I don't understand the way you are using Vowel... variables, you can omit them:
1
2
3
4
5
6
7
//instead of
cout << "U is a Vowel" << endl;
VowelU = - ( 0.01 * log ( 0.01 ) ) ;
lgResult = lgResult + VowelU;
//this is shorter
cout << "U is a Vowel" << endl;
lgResult -= 0.01 * log ( 0.01 );
but that is not an error
Last edited on
No, cmath is the C++ header...math.h is the C header...hence once one h.
Topic archived. No new replies allowed.