Strings

Pages: 12
well this is what i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string.h>
#include <cctype>

using namespace std;

int main(int argc, char *argv[])
{
    int exit=0, i=0;
    char input[30];
    printf("enter a string less than 30 chars long \n");
    while(exit!=1){
     cin >> input;
     cout << "original " << input << '\n';
     for(i=0;input[i];i++){
         if(!isspace(input[i])){     // this one doesn't work
         cout << (int)(toupper(input[i])-'A'+1) << " ";
         }
     }    
     cout << '\n';
     cin >> exit;
    }
    return 0;
}

and i have tried some of the other suggestions for ignoring spaces as well
Last edited on
Eww...printf();

First off, printf() needs the stdio.h header file, not iostream...

...and secondly, it's retarded. This returns garbage:
printf("%s", 6);
And why did you put both printf and cout in the same program anyway?

i dont know... i was sort just droning away... the app stops working before i can view the 'garbage', please enlighten me.
Last edited on
Where did printf("%s", 6); come from?
That prints garbage (or generates a segfault) because the value '6' is taken as the address of a null-terminated character string (char[]) to display. On an x86, that address is protected. (And it doesn't contain textual data.)

If I may, a few corrections/improvements:
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
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    bool done = false;  // (use boolean variables for boolean values)
    while (!done)
    {
        string input;  // (use std::strings instead of char[])

        // Get the user's input string
        cout << "Please enter something> " << flush;
        getline( cin, input );  // (use std::getline() to obtain user input)

        // Display the user's input string containing nothing but alphabetic characters
        cout << "original: ";
        for (unsigned n = 0; n < input.length(); n++)
        {
            if (isalpha(input[n]))  // (use std::isalpha() to make sure
                cout << input[n];   //  only alphabetic characters are printed)
        }
        cout << endl;

        // Display the alphabetic index of each alphabetic character
        // to the user using Hammurabi's technique
        cout << "alphabetic indices: ";
        for (unsigned n = 0; n < input.length(); n++)
        {
            if (isalpha(input[n]))
                cout << (int)(toupper(input[n])-'A'+1) << " ";
        }
        cout << endl;

        // Does the user want to play again?
        cout << "Would you like to do it again (y/[n])? " << flush;
        getline( cin, input );
        // (we're done if the user just pressed ENTER
        //  or typed anything that doesn't start with a Y)
        done = !input.empty() && (toupper(input[0])!='Y');
    }
    return 0;
}


Hope this helps.
thank you duoas... i wondering if you would enter the conversation.
is there a way to go backwards... ints to chars? I tried to reverse hammurabi's code but i dont seem to understand it all the way.
nevermind i got it... thank you all but mostly hammurabi and duoas.
> Where did this come from?

Sorry Duoas, that was a "for instance" thing. >_>
LOL, yes, I knew that. I was only explaining to the OP why it was failure. I was just wondering where the idea that he would print garbage came from, since everything was a char*... that's all.

Silly words. They never sound the way they're meant....

:-)
Topic archived. No new replies allowed.
Pages: 12