Little Program

Posts 1–15 of 21 · Page 1 of 2
Little Program
Hello mpgh'ers,

I have a problem with my C++ program.

This was the assignment:

Write a program that reads keyboard input to the @ symbol and that echoes the input except for digits, converting each uppercase character to lowercase, and vice versa.
And I wrote this:

[HIGHLIGHT=Java]#include <iostream>
#include <cctype>

using namespace std;

int main()
{
char ch;
char line[100];

int i = 0;

cin.get(ch);
while(ch != '@')
{
if(isdigit(ch))
i -= 1;
else if(islower(ch))
{
toupper(ch);
line[i] = ch;
}
else if(isupper(ch))
{
tolower(ch);
line[i] = ch;
}
else
line[i] = ch;
i++;
cin.get(ch);
}

line[i] = '\0';
cout << line;

cout << endl;
system("pause");

return 0;
}[/HIGHLIGHT]

But it shows this:



Anyone know what wrong?

Edit: Changed the code to what I have now. And everything works except the toupper() and tolower().
Ask yourself what this really means in your code you have now.
Code:
line[i] = ch;
Quote Originally Posted by SERG View Post
Ask yourself what this really means
Code:
line[i] = ch;
It means that the letter that is selected in the line array, gets assinged the same letter as in ch.
And that should work.
try this. and i am aware that if an IF statement has one line you dont need to but its good practice. Also it could be that since you didnt specify a delim its using the /n or newline as a delim by default. And since you dont clear the stream its also displaying the garabe kept after your cin.get() (If i remember correctly. P.S Im on codiene pills )
Code:
  else
            {
                        line[i] = ch;
             }
        i++;
        cin.get(ch);
Quote Originally Posted by wtfiwantthatname View Post
try this. and i am aware that if an IF statement has one line you dont need to but its good practice. Also it could be that since you didnt specify a delim its using the /n or newline as a delim by default. And since you dont clear the stream its also displaying the garabe kept after your cin.get() (If i remember correctly. P.S Im on codiene pills )
Code:
  else
            {
                        line[i] = ch;
             }
        i++;
        cin.get(ch);
So what should my entire code be?
ill give you this much(If you wanna keep it just as a while.) Ill help. Im just not going to do your work for you.

EDIT: Your also using a c-string with out terminating it.

Code:
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char ch;
    char line[100];

    int i = 0;
    cin.get(ch);
   while(ch != '@')
   {
        if(isdigit(ch))
            ;
        else if(islower(ch))
        {
            toupper(ch);
            line[i] = ch;
        }
        else if(isupper(ch))
        {
            tolower(ch);
            line[i] = ch;
        }
        else
        {
            line[i] = ch;
        }
        i++;
        cin.get(ch);
       }
    cout << line;
    return 0;
}
Quote Originally Posted by wtfiwantthatname View Post
ill give you this much(If you wanna keep it just as a while.) Ill help. Im just not going to do your work for you.

EDIT: Your also using a c-string with out terminating it.

Code:
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char ch;
    char line[100];

    int i = 0;
    cin.get(ch);
   while(ch != '@')
   {
        if(isdigit(ch))
            ;
        else if(islower(ch))
        {
            toupper(ch);
            line[i] = ch;
        }
        else if(isupper(ch))
        {
            tolower(ch);
            line[i] = ch;
        }
        else
        {
            line[i] = ch;
        }
        i++;
        cin.get(ch);
       }
    cout << line;
    return 0;
}
All you did is add "{" and "}"? That doesn't change a lot right?
Quote Originally Posted by Stupid Boy View Post
All you did is add "{" and "}"? That doesn't change a lot right?
Your line buffer isn't null terminated, so cout reads past the actual end of the string inside.

Code:
line[i] = '\0'; // null terminates line
before the cout should work.
Quote Originally Posted by mmbob View Post


Your line buffer isn't null terminated, so cout reads past the actual end of the string inside.

Code:
line[i] = '\0'; // null terminates line
before the cout should work.
Oh yeah, thanks.
I forgot about that.

That fixes the not showing the weird symbols, but why aren't the letters uppercasing and lowercasing?
Edit: I btw know it's something with the toupper() and tolower(). They are all that don't work. So am I using them wrongly or something?
Quote Originally Posted by Stupid Boy View Post
Oh yeah, thanks.
I forgot about that.

That fixes the not showing the weird symbols, but why aren't the letters uppercasing and lowercasing?
Edit: I btw know it's something with the toupper() and tolower(). They are all that don't work. So am I using them wrongly or something?
Look at what toupper() and tolower() return. Where are the return values going?
Quote Originally Posted by Stupid Boy View Post
All you did is add "{" and "}"? That doesn't change a lot right?
I said id help not that i would do it for you. I changed that because its just good practice to always use { and } even if your if statement is one line.
Quote Originally Posted by mmbob View Post


Look at what toupper() and tolower() return. Where are the return values going?
I'm sorry but I don't understand that.
toupper() and tolower() are part of the cctype header, so I don't have the function.

Quote Originally Posted by wtfiwantthatname View Post
I said id help not that i would do it for you. I changed that because its just good practice to always use { and } even if your if statement is one line.
Owh okay, thanks then.
Quote Originally Posted by wtfiwantthatname View Post
I said id help not that i would do it for you. I changed that because its just good practice to always use { and } even if your if statement is one line.
You didn't help with his problem at all.

Quote Originally Posted by Stupid Boy View Post
I'm sorry but I don't understand that.
toupper() and tolower() are part of the cctype header, so I don't have the function.
They don't change their parameter. They return the new character. Set ch equal to what toupper() and tolower() return.
Quote Originally Posted by mmbob View Post

They don't change their parameter. They return the new character. Set ch equal to what toupper() and tolower() return.
Ah thanks I got it.
It was supposed to be:

Code:
ch = toupper(ch)
and

Code:
ch = tolower(ch)
You sir need to understand what MSDN is, And that a book is better then a retarded teaches that tastes you only CopyPasta.
Posts 1–15 of 21 · Page 1 of 2
This thread is closed for replies.

Tags for this Thread

None

Talk with us