Thread: Too much input?

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Too much input?

    When trying to collect data from a user my program seems to skip collecting a string. I'm using GCC if that helps.
    Code:
    	int order[3];/*rotor order*/
    	char pos[3];/*starting position of the chosen rotors*/
    	char rings[3];/*adjustable rings*/
    	char plaintext[256];/*message to be encrypted*/
    	
    	printf("Which 3 rotors would you like to use in order? (1 through 5)\n");
    	scanf("%i %i %i",&order[0],&order[1],&order[2]);/*accept user choices of rotors, can use multiple of the same rotor*/
    	
    	printf("What letters would you like to start on?\n");
    	scanf("%c %c %c",&pos[0],&pos[1],&pos[2]);/*collect starting positions*/
    	pos[0]=toupper(pos[0]);
    	pos[1]=toupper(pos[1]);
    	pos[2]=toupper(pos[2]);
    	
    	printf("Where would you like to place the rings?(3 letters)\n");
    	scanf("%c %c %c",&rings[0],&rings[1],&rings[2]);
    	
    	printf("What is your message?\n");
    	scanf("%c",plaintext);   /*SKIPS THIS LINE*/
    	
    	printf("Your message is encoded as:\n");

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, aside from your string collection code actually reading a single character instead of a string (Try using %255[^\n] instead of %c), I would wager that the previous call to scanf left the a newline in the stream that the next call is terminating on. Check the FAQ for good ways of getting user input.
    My best code is written with the delete key.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Thank you very much for your help! I finally got my program to work correctly. I was trying to write a program that simulates the German enigma machine used in World War II. You set various setting and type in message and it encodes it for you. Then you reset the machine type in your cyphertext and it gives you the plaintext. I'll post it for giggles:
    Code:
    #include<stdio.h>/*Needed for input/output*/
    #include<string.h>/*needed for strlen()*/
    
    int main(int argc, char *argv[])
    {
    	/*Varialbe Declarations*/
    	char rotor[5][26]=
    	{"EKMFLGDQVZNTOWYHXUSPAIBRCJ",/*Rotor I*/
    	"AJDKSIRUXBLHWTMCQGZNPYFVOE",/*Rotor II*/
    	"BDFHJLCPRTXVZNYEIWGAKMUSQO",/*Rotor III*/
    	"ESOVPZJAYQUIRHXLNFTGKDCMWB",/*Rotor IV*/
    	"VZBRGITYUPSDNHLXAWMJQOFECK"};/*Rotor V*/
    	char ref[26]="YRUHQSLDPXNGOKMIEBFZCWVJAT";/*reflector*/
    	char notch[5]="QEVJZ";/*notches in the rotors*/
    	int order[3];/*rotor order*/
    	char pos[3];/*starting position of the chosen rotors*/
    	char rings[3];/*adjustable rings*/
    	char plaintext[256];/*message to be encrypted*/
    	int i, j,count=0;/*dummy variables for encoding each character and incrementing rotors*/
    	int n=0;
    	int ch,length;
    	int flag=0;
    	char junk;/*some stream chracters are causing bugs*/
    	
    	printf("Which 3 rotors would you like to use in order? (1 through 5)\n");
    	scanf("%i %i %i",&order[0],&order[1],&order[2]);/*accept user choices of rotors, can use multiple of the same rotor*/
    	scanf("%c",&junk);
    	printf("What letters would you like to start on?\n");
    	scanf("%c %c %c",&pos[0],&pos[1],&pos[2]);/*collect starting positions*/
    	
    	pos[0]=toupper(pos[0]);
    	pos[1]=toupper(pos[1]);
    	pos[2]=toupper(pos[2]);
    	
    	scanf("%c",&junk);
    	printf("Where would you like to place the rings?(3 letters)\n");
    	scanf("%c %c %c",&rings[0],&rings[1],&rings[2]);
    	rings[0]=toupper(rings[0]);
    	rings[1]=toupper(rings[1]);
    	rings[2]=toupper(rings[2]);
    	scanf("%c",&junk);/*collect some garbage*/
    	
    	printf("What is your message?\n");
    	fgets(plaintext,255,stdin);
    	
    	printf("Your message is encoded as:\n");
    
    	length=strlen(plaintext);
    	for(;count<length;count++)/*keep reading in letters until the end of the string*/
    	{
    		ch=plaintext[count];
    		ch=toupper(ch);
    		if (!isalpha(ch))
    			continue;
    		
    		/* Step up first rotor */
    		pos[0]++;
    		if (pos[0]>'Z')
    			pos[0] -= 26;
    
    		/* Check if second rotor reached notch last time */
    		if (flag)
    		{
    			/* Step up both second and third rotors */
    			pos[1]++;
    			if (pos[1]>'Z')
    				pos[1] -= 26;
    			pos[2]++;
    			if (pos[2]>'Z')
    				pos[2] -= 26;
    			flag=0;
    		}
    
    		/*  Step up second rotor if first rotor reached notch */
    		if (pos[0]==notch[order[0]-1])
    		{
    			pos[1]++;
    			if (pos[1]>'Z')
    				pos[1] -= 26;
    			/* Set flag if second rotor reached notch */
    			if (pos[1]==notch[order[1]-1])
    				flag=1;
    		}
    
    
    		/*  Rotors (forward) */
    		for (i=0; i<3; i++)
    		{
    			ch += pos[i]-'A';
    			if (ch>'Z')
    				ch -= 26;
    
    			ch -= rings[i]-'A';
    			if (ch<'A')
    				ch += 26;
    
    			ch=rotor[order[i]-1][ch-'A'];
    
    			ch += rings[i]-'A';
    			if (ch>'Z')
    				ch -= 26;
    
    			ch -= pos[i]-'A';
    			if (ch<'A')
    				ch += 26;
    		}
    
    		/*  Reflecting rotor */
    		ch=ref[ch-'A'];
    
    		/*  Rotors (reverse) */
    		for (i=3; i; i--)
    		{
    			ch += pos[i-1]-'A';
    			if (ch>'Z')
    				ch -= 26;
    
    			ch -= rings[i-1]-'A';
    			if (ch<'A')
    				ch += 26;
    
    			for (j=0; j<26; j++)
    				if (rotor[order[i-1]-1][j]==ch)
    					break;
    			ch=j+'A';
    
    			ch += rings[i-1]-'A';
    			if (ch>'Z')
    				ch -= 26;
    
    			ch -= pos[i-1]-'A';
    			if (ch<'A')
    				ch += 26;
    		}
    		
    
    		n++;
    		putchar(ch);
    		if (n%5==0)
    		{
    			if (n%55==0)
    				putchar('\n');
    			else
    				putchar(' ');
    		}
    	}
    	return 0;
    }

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    you know its much easier to read source thats been organized...ie you dont have to do everything in main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM