5 Replies - 515 Views - Last Post: 24 May 2013 - 02:08 PM Rate Topic: -----

#1 prog_gig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-May 13

Strange output in C

Posted 23 May 2013 - 05:05 PM

Hi, ive got some problem with my code, when I print out the name it gives me strange characters like this: ��. Do any of you guys could help me please ? I'm really stuck :(/>

P.S. Sorry for my english, im a french speaker.




#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct message
{
  char chaine[256];
  int sum ;
};

void printStructure(struct message mess);

char getStructure(){
  struct message m;
  int sum = 0;
  int i = 0;
  char c;

  printf("Enter a name: ");
  scanf("%c", &c);
  fflush(stdin);
  while((c = getchar()) != '\n'){
    m.chaine[i]= c;
    i++;
  }
  fflush(stdin);
  m.chaine[i] = '\0';

  i=0;

   while(m.chaine[i] != '\0'){
   sum += m.chaine[i];
   i++;
   }

  printf("Checksum = %i \n", sum);


  return m.chaine[i];
}
  
void printStructure(struct message m){
 
  printf("Your name is: %s", m.chaine);  
  printf("\n");
  
}

int menu(){ 

 printf("----------------------------------------\n");
 printf (" 1. Saisir la structure \n 2. Afficher la structure \n 3. Inverser la chaine de la structure \n");
 printf(" 4. Afficher le nombre de mots \n 5. Supprimer un caractere \n 6. Remplacer un caractere \n 7. Quitter \n");
 printf("-----------------------------------------\n");

  int choice;
  struct message m;
  int i = 0;
  

  printf("Choose a option: ");
  scanf("%i", &choice);
  fflush(stdin);

  switch (choice){

 case 1:
   m.chaine[i] = getStructure();
   menu();
   break;

  case 2:
    printStructure(m);
    menu();

  //Option quitter le programme
  case 7:
    printf("Bye \n");
    exit(0);
  }
 return 0;
}

int main(){
   menu();

  return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Strange output in C

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Strange output in C

Posted 23 May 2013 - 06:47 PM

I think you need to study your documentation for scanf(). Let's look at the following snippet:
  printf("Enter a name: ");
  scanf("%c", &c);
  fflush(stdin);

You seem to be expecting the user to enter a name, yet you only try to retrieve one character. Does that sound correct? Maybe you should consider using a string to hold the name?

Also fflush() is only defined to work with output streams, not input streams.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 prog_gig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-May 13

Re: Strange output in C

Posted 24 May 2013 - 10:01 AM

If you look correctly at the code you'll see that I used a getchar after. That means that the user can put any character he wants until he press enter.
 printf("Enter a name: ");
  scanf("%c", &c);
  fflush(stdin);
  while((c = getchar()) != '\n'){
    m.chaine[i]= c;
    i++;
  }
  fflush(stdin);
  m.chaine[i] = '\0'; 

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Strange output in C

Posted 24 May 2013 - 10:07 AM

Except you seem to forget that you already have a new line in the input buffer, left there by your scanf() call.

The fflush() call on most systems doesn't affect the input buffer. As I said in my earlier post using fflush() on an input stream is undefined in the standard, that function is only designed to work with output streams.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 prog_gig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-May 13

Re: Strange output in C

Posted 24 May 2013 - 02:00 PM

Okaay thank you ! I have an other echew. I dont understand why that code doesnt work. It's supposed to count the number of words in a sentence.
void nombreMots(struct Message tab[], const int n1){
int i;
int increase = 0;

while(tab[i].chaine != '\0'){
if(tab[i].chaine == ' '){
++increase;
++i;
    }
	}	
printf("Votre chaine comporte %i mot(s) \n", increase+1);

}

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Strange output in C

Posted 24 May 2013 - 02:08 PM

So why doesn't it work?

Do you get a compile error?

If not the provide a sample sentence and what output your program produced with that input.

A small complete program, that illustrates your problem would be beneficial.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1