0

Once again i find myself relatively lost and seeking knowledge from my peers. What I am needing to do is to write a program that takes in an encoded language that adds the leters 'u' 't' after ever consonant and output into english. So the input Hutelutluto would be outputted as hello. At first I thought i had it figured out, but then my professor said i had to store the initial input in a character array and display it. Then use that character array to output the modified translation.

I have tried several different angles, one trying to modify my readstring function to fit my modification parameters. It always ends up becoming a mess and giving me unexpected outputs.

Essentially I believe that I need help feeding the character array into the while loop, but when I try I get an error stating that i have a pointer comparison to integer error.

Here is my edition of my code where i believe I was the closest to solving the problem. At the moment the while works independently from the readstring function. I am sure I am overthinking the problem, but i just not sure how to work out the kinks. :

/*

Tut language
By: Steven

*/

# include <stdio.h>
void readstring(char *, int);

int main (void){
  char input [50];
  char output;
  char trash;

//initiation
printf("\n\t*Hi! Welcome to the assemble of Tut*\n");
printf("I can help you decode/encode a message to and from the code called Tut!\n");
printf("Enter a sentence to be translated frome Tut - English: ");
readstring(input, 50);
printf("Your Tut sencence is: %s \n",input);








  while (output != '\n') {
    output = getchar();
    if(output == '\n'){//escape sequence
      break;
    }
      if((output != 'a') && (output != 'e') && (output != 'i') && (output != 'o') && (output != 'u') && (output != 'y') && (output != ' ')){
        putchar(output);
        trash = getchar();
        trash = getchar();
      }else{
        putchar(output);
      }
  }
  return 0;
}// end main

//function lists start
void readstring(char * buffer, int size) {
  int x;
  char c = getchar( );

  if( c == '\n' ) {
    c = getchar( );
  }

  for(x = 0 ; (x < (size-1)) && c != '\n' ; x++) {
    buffer[x] = c;
    c = getchar( );
  }

  buffer[x] = '\0';
}

Any help or feedback will be greatly appreciated! Thank your for your time!

p.s.

After taking your advice into consideration i edited my code, but it seems as if it is ignoring all input after the first. I even tried changing the !='\n' condition to just i < 50, but i got the same results.

Line of code after taking Tim's advice:

    for (i = 0; input [i] != '\n'; ++i){
    output = input[i];
      if((output != 'a') && (output != 'e') && (output != 'i') && (output != 'o') && (output != 'u') && (output != 'y') && (output != ' ')){
        putchar(output);
        trash = getchar();
        trash = getchar();
      }else{
        putchar(output);
      }
  }
scubasteve7
  • 57
  • 2
  • 2
  • 5
  • So i figure that my issue is with the trash=getchar(); declaration. I initially used that to get around the next two character inputs if the character entered was not a vowel. But seeing that I have to store the initial message in a character array, i have no idea how I am going to skip those next two character? – scubasteve7 Dec 08 '15 at 00:14

3 Answers3

0

The main problem is in your while loop. In this loop, you are using getchar, which tries to read from stdin. However, you have already read your input, and it is stored in buffer.

So, you should get your characters from buffer instead of reading them again.

Richard St-Cyr
  • 970
  • 1
  • 8
  • 14
  • I did mention that at the moment the two wok independently, im needing to figure out how to combine both of their utilities essentially. – scubasteve7 Dec 07 '15 at 21:48
0

Your readstring function already calls getchar to read characters from the user's terminal, so you should not need to call it again after readstring is done. Instead, use a for loop to set output to each character of the input string in turn:

int i;
for i = 0; input[i] != '\n'; ++i {
    output = input[i]
    if((output != 'a') && (output != 'e') && (output != 'i') && (output != 'o') && (output != 'u') && (output != 'y') && (output != ' ')){
        ...
Tim Pierce
  • 5,514
  • 1
  • 15
  • 31
  • Ok that makes sense, its pretty much what i was looking for but, is there a reason why the for loop stops prematurely at the first letter h? For example here is my console output: *Hi! Welcome to the assemble of Tut* I can help you decode/encode a message to and from the code called Tut! Enter a sentence to be translated frome Tut - English: hutelutluto Your Tut sencence is: hutelutluto h – scubasteve7 Dec 07 '15 at 21:47
  • I don't know why the loop is terminating prematurely. Look through it closely to see if it is calling `break` at the wrong time, or if it is ignoring characters beyond the first one for some reason, or changing the input or the i variable when it shouldn't. – Tim Pierce Dec 07 '15 at 21:52
  • It is ignoring all character after the first. I believe this to be because of how i was throwing out the next two character inputs with trash = getchar();. How would i get around this problem without using trash = getchar (); Ill post the algorithm above and see if you can spot anything. – scubasteve7 Dec 08 '15 at 00:11
  • I think the question here is: why are you calling `getchar()` at all at this point? What are you trying to achieve? Remember: once your `readstring()` function is done collecting input from the user's terminal, you should not need to call `getchar()` again at all. Think carefully about what you want to accomplish, then think about where your program is storing its data so you can figure out how to achieve that effect. – Tim Pierce Dec 08 '15 at 15:30
0

… in a character array, i have no idea how I am going to skip those next two character?

You skip characters by incrementing the array index. Change

        trash = getchar();
        trash = getchar();

to

        input[++i] && input[++i] || --i;    // step back if at end of string

Also, since the input string is terminated by \0 rather than \n, change

    for (i = 0; input [i] != '\n'; ++i){

to

    for (i = 0; input[i]; ++i)
    {
Armali
  • 18,255
  • 14
  • 57
  • 171