-
help with programs
plz can i get help with that:
Write a C++ program with a case structure.
1.
The program reads in a character from the user, then translates it into a different character according to the following rules:
characters
1. A, B, C will be translated to characters z, y, x respectively;
characters
2. 6, 7, 8, 9 will all be translated to character 5;
3. the space character ' ' will be translated to underscore '_'
4. all other characters remain unchanged.
The program then displays the translated character
2
Write a C++ program that reads from keyboard 3 words, with proper input prompt, and then display them in the order of the word length (shortest word first).
3
Write a C++ program with a loop structure that reads in 5 integers one by one, calculate the sum of the even values that are read and display the final result.
-
please read this and this before posting to the board. noone is going to do your homework for you, but specific questions are certainly welcome. a good start would be to post the code you have thus far and show where you are stuck.
-
The trick to ANY program is to write it a little at a time!
Start-out with a little Hello World program, and add one or two lines at a time, test-compiling and test-running each time. This takes a little practice, you have to skip-around a bit... For example, if you just enter the first-half of a program, it won't compile....
Sometimes it's helpful to put in some extra temporary cout statements, so you can "see' what your program is doing. For example, in your program you might have a temporary cout statement that says "User has input A, B or C... need to translate".
Note - Remember this technique when you start learning functions. Start-out with an empty do-nothing function. (You might have to make the function return a "fake" value.) When the program compiles and runs, make the function actually do something.
When As you gain experience, you can write bigger chunks of code between testing. But, your programs will get bigger too. So, you'll almost never write an entire program before testing!
In your first program, you can replace "Hello World" with "Enter a character".
When that works, write the code to get a character from the user, and the code at the end of the program to display it (or to display the translated character).
Next, you can write (and test) the if-statement to check for A, B, or C.
Etc., etc., etc.
You should also plan-out your program with a flow-chart, or psudo-code, etc.
I usually "outline" the program in comments before I get started, like this:
// Read a character from the user ;
// Is the Character ABC?;
// If so, translate it to x, y,z;
// Is the character 6, 7, 8. or 9?;
// If so, translate to 5;
// Is it a space?
// If so, translate it to underscore;
// Display the result.
//END
After I have the comments, I start filling-in and testing the actual code. Usually the comments need editing too.... There's usually some difference between saying what the program is supposed to do, and proper comments explaining what the program is actually doing.
FYI - That's an informal technique that works for me, but it's not the kind of formal design and documentation that's tought in computer science or required in some formal organizations.
-
i tried the part one and i still got problems doing it:
Code:
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << " enter a character:";
cin >>ch;
if (ch =='6'||ch == '7'||ch == '8'|| ch == '9')
{
ch= '5';
cout << "character converted to :" << ch;
}
if ( ch == 'A')
ch='z';
else if (ch == 'B')
ch ='y';
else if (ch == 'C')
ch ='x' ;
else if ( ch == 'space')
ch = '_';
cout << "character converted to:" <<ch;
else
cout << " the character is:"<<ch;
}
system("PAUSE");
return 0;
}
that's what i've done can i get your help
-
>> i tried the part one and i still got problems doing it:
remember: informative posts usually get better responses: 'I keep getting a compilation error X on line Y' or 'when I print debug statements the logic/runtime error seems to occur somewhere around line Z' rather than 'It doesn't work'.
Code:
else if ( ch == 'space')
ch = '_';
cout << "character converted to:" <<ch;
- 'space' is not a character, assuming you meant ' '.
- the statements following your else if clause must be grouped together either with brackets or by separating them with commas - otherwise, only the first statement gets associated with it
-
i tried to tape ' ' insread of 'space' but i got an error message saying typing empty value constant how do i solve this problem
plz i need help as soon as possible
-
Put a space between the single quotes and it won't be an empty value constant.
-
i did do that but i still got the same message
-
but i tried this :
Code:
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << " enter a character:";
cin >>ch;
if (ch =='6'||ch == '7'||ch == '8'|| ch == '9')
{
ch= '5';
cout << "character converted to :" << ch;
}
if ( ch == 'A')
ch='z';
else if (ch == 'B')
ch ='y';
else if (ch == 'C')
ch ='x' ;
cout << "character converted to:"<< ch;
if ( ch= ' ')
{
ch = '_';
cout <<"character converted to:";
ch=cin.get();
}
system("PAUSE");
return 0;
}
but when i type ' ' int the output nothing happen
-
>> if ( ch= ' ')
That should be ==.
Also, you cannot read in a space with cin >>, so ch will never be ' ' in that code. You need to read in with cin.get to be able to read in a space.
-
can i get any hints in doing this
Code:
Write a C++ program that reads from keyboard 3 words, with proper input prompt, and then display them in the order of the word length (shortest word first).
-
thank you for your help daved , Sebastiani and
DougDbug
-
just a tip, it's always a good idea to reverse the comparion
ex. if ('5'==ch), this would prevent errors like , if (ch='5')
-
i need a help how to create a loop to make the sum of even number
-
Quote:
the sum of even number
Pray tell, what does that mean? The sum of an even number of numbers? To sum to an even number? To even out an uneven number?