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;

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:

# 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

Dani AI

Generated

A quick, practical guide that fills gaps in the replies above and fixes the key mistakes in the two tasks.

Problem 1 (single-character translation). Read exactly one character with cin.get() (so spaces count). Use a switch on that char for the explicit mappings ('A','E','I','O','U' -> lowercase, ' ' -> '_') and use std::isdigit for digits to translate to '#'. Watch out: pass static_cast<unsigned char>(ch) to std::isdigit to avoid undefined behavior on negative char values. Also remember that if you used operator>> earlier the input buffer may already hold a newline; consume it (or prompt fresh) before calling cin.get().

Example (concise, modern C++ style):

#include <iostream>
#include <cctype>

int main() {
    char ch;
    std::cout << "Enter a char: ";
    ch = std::cin.get();
    char out = ch;
    switch (ch) {
      case 'A': out = 'a'; break;
      case 'E': out = 'e'; break;
      case 'I': out = 'i'; break;
      case 'O': out = 'o'; break;
      case 'U': out = 'u'; break;
      case ' ': out = '_'; break;
      default:
        if (std::isdigit(static_cast<unsigned char>(ch))) out = '#';
    }
    std::cout << "Translated: " << out << '\n';
}

Problem 2 (three words, print longest, shortest, then remaining). The root error in the OP code was using char for whole words. Use std::string. A simple, reliable approach: put the three std::string values into a small container and sort by length(); std::stable_sort keeps original order for equal lengths. After sorting ascending, output array[2] (longest), array[0] (shortest), then array[1]. If your compiler lacks C++11 lambdas, use a named comparator function.

Notes tied to the thread: was correct to flag the char mistake; ’s pointer to <cctype> is useful for digit checks; ’s sorting idea is the cleanest approach here; ’s long if solution works but is more verbose and harder to maintain. Test edge cases (leading/trailing spaces, equal lengths) and avoid nonportable helpers like system("pause").

Recommended Answers

All 7 Replies

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.

Regarding your first problem, you'll likely find the cctype library helpful:

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!

#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;
      }
commented: Congratulations for doing someone else's homework. -3

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.

=(

It works doesn't it?:sad:

i m yet to make my mind about the first question but for the second question.
make a array of string,

string array[3];

use a bubble sort or any kind of sort to sort those arrays in ascending order.

for(i=0;i<3;i++){
        for(j=i;j<3;j++){
                if(array[i].length()>=array[j].length()){
                        swap(array[i],array[j]);}
        }
}

and print the first,third and second location of array by using

cout<<"shortest "<<array[0]<<endl;
cout<<"longest"<<array[2]<<endl;
cout<<"the last"<<array[1]<<endl;

for the shortest longest and the remaining word respectively

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.