| | |
Need Help !!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 0
Hey all
names william
I am in my first year uni - doin bachelor of computin and hence dont no much bout programming
I have to submit this assingment tomorow and am confused as to what I am supposed to be doin
I would appriciate any elp from any1
I will put the question up first and then what I have written pls point me in the right direction
Question
Write a C++ program with a case structure. The program reads in a character from the user, then display its translation according to the following rules:
i. characters A, E, I, O, U will be translated to characters a, e, i, o, u respectively;
ii. the space character ' ' will be translated to underscore '_'
iii. digits 0, 1, 2, .., 9 will all be translated to '#'
iv. all other characters remain unchanged
The program then displays the translated character.
NOTE: You can make use of cin.get() to get the next character even if that character is a white space, see for example,
MY ANSER:
( Note - I am using Dev c++ as the compiler - uni standards)
#include<iostream>
using namespace std;
QUESTON:
Write a C++ program that reads from keyboard 3 words, with proper input prompt. Then for these 3 words that were read, the program displays first the word of the longest length, then the word of the shortest length, and finally the remaining last word. You may assume that these 3 words are all different in lengths, or consider a word to be of the longest length if no other words exceed it in length.
MY ANSWER:
I Know this is a lot to be asking but please help
names william
I am in my first year uni - doin bachelor of computin and hence dont no much bout programming
I have to submit this assingment tomorow and am confused as to what I am supposed to be doin
I would appriciate any elp from any1
I will put the question up first and then what I have written pls point me in the right direction
Question
Write a C++ program with a case structure. The program reads in a character from the user, then display its translation according to the following rules:
i. characters A, E, I, O, U will be translated to characters a, e, i, o, u respectively;
ii. the space character ' ' will be translated to underscore '_'
iii. digits 0, 1, 2, .., 9 will all be translated to '#'
iv. all other characters remain unchanged
The program then displays the translated character.
NOTE: You can make use of cin.get() to get the next character even if that character is a white space, see for example,
MY ANSER:
( Note - I am using Dev c++ as the compiler - uni standards)
#include<iostream>
using namespace std;
C++ Syntax (Toggle Plain Text)
int main() { char ch; cout << "Enter a char:"; ch=cin.get(); cout << "1st char has code " << unsigned(ch) << " and is \"" << ch << "\"\n"; system("pause"); return 0; }
QUESTON:
Write a C++ program that reads from keyboard 3 words, with proper input prompt. Then for these 3 words that were read, the program displays first the word of the longest length, then the word of the shortest length, and finally the remaining last word. You may assume that these 3 words are all different in lengths, or consider a word to be of the longest length if no other words exceed it in length.
MY ANSWER:
C++ Syntax (Toggle Plain Text)
# include <iostream> using namespace std; int main() { char word1, char word2, char word3; cout << "Enter word1: " <<endl; cin >>word1; cout <<"Enter word2: " <<endl; cin >>word2; cout << "Enter word3: " <<endl; cin >>word3; system ("pause"); return 0; }
I Know this is a lot to be asking but please help
Last edited by John A; Apr 10th, 2008 at 3:10 am. Reason: added code tags
Your first program is incorrect in that it doesn't do even close to what the question was asking. Your second program is a bit better (but not by that much). I don't know if you've learned how to use C++-style strings, but if you have, you should definitely use them. In any case, you're trying to input an entire word into a single character, which is completely wrong. At the very minimum you should make a char array (would have a syntax of the following:
char array[num_chars] ). I know I sound a bit negative, but you really shouldn't be coming here a day before it's due and then begging for help to make it work. If you were having problems with your code, you should have tried to get help right away. No one's going to do your assignment for you, and at the rate you're going, it's unlikely you'll have much to hand in. Last edited by John A; Apr 10th, 2008 at 3:21 am.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
Regarding your first problem, you'll likely find the cctype library helpful:
http://www.cplusplus.com/reference/clibrary/cctype/
You're going to have to elaborate about what you mean by "case". Are you supposed to implement a "switch" statement with some "case" statements inside of it, or are you talking about something else? I think this problem calls for a straight if-else if - else if - else structure and I would use that unless a switch is required.
http://www.cplusplus.com/reference/clibrary/cctype/
You're going to have to elaborate about what you mean by "case". Are you supposed to implement a "switch" statement with some "case" statements inside of it, or are you talking about something else? I think this problem calls for a straight if-else if - else if - else structure and I would use that unless a switch is required.
This is a little long, but it works!
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> using namespace std; int main() { string word1, word2, word3; cout << "Enter word1: " <<endl; cin >>word1; cout <<"Enter word2: " <<endl; cin >>word2; cout << "Enter word3: " <<endl; cin >>word3; int Longest, Shortest; // Get the longest word if (word1.length() > word2.length() && word1.length() > word3.length()) { Longest = 1; cout << "\nLongest word: " << word1; } if (word2.length() > word1.length() && word2.length() > word3.length()) { Longest = 2; cout << "\nLongest word: " << word2; } if (word3.length() > word1.length() && word3.length() > word2.length()) { Longest = 3; cout << "\nLongest word: " << word3; } // Get the shortest word if (word1.length() < word2.length() && word1.length() < word3.length()) { Shortest = 1; cout << "\nShortest word: " << word1; } if (word2.length() < word1.length() && word2.length() < word3.length()) { Shortest = 2; cout << "\nShortest word: " << word2; } if (word3.length() < word1.length() && word3.length() < word2.length()) { Shortest = 3; cout << "\nShortest word: " << word3; } // Get the word in the middle if (Shortest == 1 && Longest == 2) { cout << "\nOther word: " << word3; } if (Shortest == 2 && Longest == 1) { cout << "\nOther word: " << word3; } if (Shortest == 1 && Longest == 3) { cout << "\nOther word: " << word2; } if (Shortest == 3 && Longest == 1) { cout << "\nOther word: " << word2; } if (Shortest == 2 && Longest == 3) { cout << "\nOther word: " << word1; } if (Shortest == 3 && Longest == 2) { cout << "\nOther word: " << word1; } cout << endl; system ("pause"); return 0; }
Last edited by TheBeast32; Apr 10th, 2008 at 5:59 pm.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
--Martin Golding
TheBeast32, may I first point you to http://www.daniweb.com/forums/announcement8-2.html. Don't hand out complete homework solutions which students can take and turn in for a grade.
Not to mention that the code that you posted is highly inefficient and contains some bad coding practices. In short, you're likely doing the original poster more harm than good with a reply like that.
Not to mention that the code that you posted is highly inefficient and contains some bad coding practices. In short, you're likely doing the original poster more harm than good with a reply like that.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
i m yet to make my mind about the first question but for the second question.
make a array of string,
use a bubble sort or any kind of sort to sort those arrays in ascending order.
and print the first,third and second location of array by using
for the shortest longest and the remaining word respectively
make a array of string,
C++ Syntax (Toggle Plain Text)
string array[3];
C++ Syntax (Toggle Plain Text)
for(i=0;i<3;i++){ for(j=i;j<3;j++){ if(array[i].length()>=array[j].length()){ swap(array[i],array[j]);} } }
C++ Syntax (Toggle Plain Text)
cout<<"shortest "<<array[0]<<endl; cout<<"longest"<<array[2]<<endl; cout<<"the last"<<array[1]<<endl;
Last edited by Arpy Giri; Aug 19th, 2008 at 5:42 am. Reason: code
![]() |
Other Threads in the C++ Forum
- Previous Thread: I'm drawing a blank
- Next Thread: Sorting Array Help
Views: 862 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






