Need Help !!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 4
Reputation: WilliamBaxter is an unknown quantity at this point 
Solved Threads: 0
WilliamBaxter WilliamBaxter is offline Offline
Newbie Poster

Need Help !!!

 
0
  #1
Apr 10th, 2008
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;

  1. int main()
  2. {
  3. char ch;
  4. cout << "Enter a char:";
  5. ch=cin.get();
  6. cout << "1st char has code " << unsigned(ch)
  7. << " and is \"" << ch << "\"\n";
  8. system("pause");
  9. return 0;
  10. }

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:
  1. # include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char word1, char word2, char word3;
  6. cout << "Enter word1: " <<endl;
  7. cin >>word1;
  8. cout <<"Enter word2: " <<endl;
  9. cin >>word2;
  10. cout << "Enter word3: " <<endl;
  11. cin >>word3;
  12. system ("pause");
  13. return 0;
  14. }

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Need Help !!!

 
1
  #2
Apr 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need Help !!!

 
0
  #3
Apr 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Need Help !!!

 
-1
  #4
Apr 10th, 2008
This is a little long, but it works!

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string word1, word2, word3;
  8.  
  9. cout << "Enter word1: " <<endl;
  10. cin >>word1;
  11. cout <<"Enter word2: " <<endl;
  12. cin >>word2;
  13. cout << "Enter word3: " <<endl;
  14. cin >>word3;
  15.  
  16. int Longest, Shortest;
  17.  
  18. // Get the longest word
  19. if (word1.length() > word2.length() && word1.length() > word3.length())
  20. {
  21. Longest = 1;
  22. cout << "\nLongest word: " << word1;
  23. }
  24. if (word2.length() > word1.length() && word2.length() > word3.length())
  25. {
  26. Longest = 2;
  27. cout << "\nLongest word: " << word2;
  28. }
  29. if (word3.length() > word1.length() && word3.length() > word2.length())
  30. {
  31. Longest = 3;
  32. cout << "\nLongest word: " << word3;
  33. }
  34.  
  35. // Get the shortest word
  36. if (word1.length() < word2.length() && word1.length() < word3.length())
  37. {
  38. Shortest = 1;
  39. cout << "\nShortest word: " << word1;
  40. }
  41. if (word2.length() < word1.length() && word2.length() < word3.length())
  42. {
  43. Shortest = 2;
  44. cout << "\nShortest word: " << word2;
  45. }
  46. if (word3.length() < word1.length() && word3.length() < word2.length())
  47. {
  48. Shortest = 3;
  49. cout << "\nShortest word: " << word3;
  50. }
  51.  
  52. // Get the word in the middle
  53. if (Shortest == 1 && Longest == 2)
  54. {
  55. cout << "\nOther word: " << word3;
  56. }
  57. if (Shortest == 2 && Longest == 1)
  58. {
  59. cout << "\nOther word: " << word3;
  60. }
  61.  
  62. if (Shortest == 1 && Longest == 3)
  63. {
  64. cout << "\nOther word: " << word2;
  65. }
  66. if (Shortest == 3 && Longest == 1)
  67. {
  68. cout << "\nOther word: " << word2;
  69. }
  70.  
  71. if (Shortest == 2 && Longest == 3)
  72. {
  73. cout << "\nOther word: " << word1;
  74. }
  75. if (Shortest == 3 && Longest == 2)
  76. {
  77. cout << "\nOther word: " << word1;
  78. }
  79.  
  80. cout << endl;
  81.  
  82. system ("pause");
  83. return 0;
  84. }
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Need Help !!!

 
1
  #5
Apr 10th, 2008
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.
"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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Need Help !!!

 
0
  #6
Aug 5th, 2008
=(
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Need Help !!!

 
0
  #7
Aug 16th, 2008
It works doesn't it?
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 12
Reputation: Arpy Giri is an unknown quantity at this point 
Solved Threads: 3
Arpy Giri's Avatar
Arpy Giri Arpy Giri is offline Offline
Newbie Poster

Re: Need Help !!!

 
0
  #8
Aug 19th, 2008
i m yet to make my mind about the first question but for the second question.
make a array of string,
  1. string array[3];
use a bubble sort or any kind of sort to sort those arrays in ascending order.
  1. for(i=0;i<3;i++){
  2. for(j=i;j<3;j++){
  3. if(array[i].length()>=array[j].length()){
  4. swap(array[i],array[j]);}
  5. }
  6. }
and print the first,third and second location of array by using
  1. cout<<"shortest "<<array[0]<<endl;
  2. cout<<"longest"<<array[2]<<endl;
  3. cout<<"the last"<<array[1]<<endl;
for the shortest longest and the remaining word respectively
Last edited by Arpy Giri; Aug 19th, 2008 at 5:42 am. Reason: code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 862 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC