Thread: me again, string flag variables

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    me again, string flag variables

    this block of code works just fine, but where it says enter <0> to quit i need to change it to enter <quit>, it's id_num is int right now, i'm guessing it's needs to be char. anyone care to offer some code modifications, or an outline of what i need to do?
    Nova
    {
    int x=1;

    printf("Enter <0> under ID number when finished\n");
    scanf("%d", &id_num[x]);
    /*while loop that quits when user enters 0 under id_num*/
    while(id_num[x] != 0) {
    printf("Enter the amount of chairs you desire to order\n");
    scanf ("%d", &num_chair[x]);
    printf("Enter the amount of sofas you desire to order\n");
    scanf ("%d", &num_sofa[x]);
    /*increments the position in the array*/
    x++;
    printf ("Enter <0> under ID number when finished\n");
    scanf("%d", &id_num[x]);
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    try this

    char id[20][5];

    printf("Enter quit to finish");
    gets(id[x]);

    if(!strcmp(id[x], "quit"))
    break;

    strcmp() compares the 2 strings it recieves and returns 0 if hey match. You will need to include the <string.h> header

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One thing to watch out for is caps. quit and QUIT are different so you will need to take that into account.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    int main()
    {
    	char id_num[1][5];
    
    	printf ("Enter <quit> under ID number when finished\n");
    	fgets(id_num[0],5,stdin);
    	printf("\nOutput: %s\n",id_num[0]);
    	return 0;
    }
    Run this code. fgets is better than gets because it protects the stream input by allowing you to control the size of the stream that it accepts. stdin means standard input.

    On another note. scanf is not good unless you take special precautions. The reason is because it can leave characters in the standard input buffer, and most critically the newline character. Infact even fgets can leave characters in the standard input buffer but not the newline. A newline left in the buffer can kill subsequent input attempts. One way to combat this is to clear the buffer. You can put this into your code after you scan:

    while(getchar() != '\n') continue;

    Also regarding validation. This should be performed on all input. It can be accomplished with scanf and a loop. If the input fails than the loop executes and the user is asked to enter input again. The loop breaks if the input is good. For example run this program:

    Code:
    int main()
    {
    	int number;
    	//Say I want a number between 0 and 100 range
    	//All other input is rejected
    	printf("Enter a number: ");
    	while( scanf("%d",&number) != 1 || number > 100 || number < 0)
    	{
    		//clear standard input buffer
    		while(getchar() != '\n') continue;
    		printf("\nOut %d is out of range. \nEnter number between 0 and 100: ",number);
    	}
    	
    	printf("\nYou entered %d, this is acceptable, thank you.\n",number);
    	return 0;
    }
    Lastly, now I see more of your program. Regarding your sort function. I can give you a simple example if you like.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 02:57 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM