Thread: Enter i to quit or i to continue

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    1

    Enter i to quit or i to continue

    Hi all, I have a c programming question I am stuck on.
    The question first asks to write a program that takes multiple values and stores the numbers in an array. The array should not be more than 10. Then sort the array in ascending order.


    once the array is sorted, the next job is to get user command:


    user command: i for insertion, q for quit


    typing 'i' prompts for one additional number
    typing 'q' quits program


    this is the problem i cant solve, i have figured out the whole program accept how to insert a user command to quit or continue after sorting the first array.


    if the user types 'i' the program gets a number input from the user and insert one additional number in the correct (ascending order) position of the array.


    i tried inserting this after the first array is sorted but did not work:


    Code:
    printf("Enter 'q' to quit or 'i' to continue\n");
      scanf_s("%c", &command);
    	  while(command!='q' || command!='Q')
    	  {
    		  while((command='i')|| (command='I'))
    		  {

    i also tried an if statement on the quit or insert and couldnt get it to work?


    Any help will be greatly appreciated, I've been stuck on this for a few days.


    here's what I have so far:


    Code:
     
    #include<stdio.h>
    int main()
    {
    	
    	int s,x,j,temp,a[20]; 
    
    
      printf("Please enter the array size of no greater than 10:\n ");//Prompt
      scanf_s("%d",&s);//Obtain array size from user
      while(s>10)
      { printf("Please enter the array size, LESS than 10\n");//Prompt
      scanf_s("%d",&s);}//Re-enter array size less than 10
    
    
      printf("Please enter %d elements, to sort into ascending order: \n",s);//Prompt
      for(x=0;x<s;x++)
          scanf_s("%d",&a[x]);//Obtain array elements
    
    
      for(x=0;x<s;x++){
          for(j=x+1;j<s;j++){
               if(a[x]>a[j]){
                   temp=a[x];
                  a[x]=a[j];
                  a[j]=temp;
               }//Sort array into ascending order
          }
      }
    
    
      printf("the strings are: \n");
      for(x=0;x<s;x++)
      { printf(" %d\n",a[x]);}//Print array in ascending order
    
    
    
    
    
    
    //Problem section -
      //where i need to prompt user "Enter 'q' to quit or 'i' to continue
    
    
    
    
    			  printf("Enter a number to insert into the array in its corresponding position:\n");//Prompt
    			  for(x=s;x<(s+1);x++)
    			  {
    				  scanf_s("%d",&a[x]);
    			  }//Obtain 1 additional number and insert into array
    				  for(x=0;x<(s+1);x++)
    				  {
    					  for(j=x+1;j<(s+1);j++)
    					  {
    						  if(a[x]>a[j])
    						  {
    							  temp=a[x];
    							  a[x]=a[j];
    							  a[j]=temp;
    						  }//Sort new array into ascending order
    					  }
    				  }
    
    
    		 
    
    
    			  printf("The new string is: \n");
    			  for(x=0;x<(s+1);x++)
    			  
    			  {
    				  printf(" %d\n",a[x]);//Print new array into ascending order
    			  
    			  }
    		  
    		  
    	  
      return 0;
      
    }

  2. #2
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Code:
    char c; 
    c = getch(); // You might need <conio.h> library to use getch();
    while( c != 'i' && c != 'q' ) // If you don't enter q or i it will keep asking you to press new key.
        c = getch();
    while( c == 'i' ) { //when you press i
        // This code executes.
        c = getch(); // Press new key for another addition to array or quit
        while( c != 'i' && c != 'q' ) // If you don't enter q or i it will keep asking you to press new key.
            c = getch();
    }
    // Goes here if you press q.
    I assumed that you allow user to make insertion as much as they want. If you want to do it once you gotta delete what I wrote inside 2nd while loop and just put your code there and at the end don't forget to type c = 'q'; or it will loop forever.
    Last edited by Vsky; 04-10-2015 at 12:50 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
              while((command='i')|| (command='I'))
    Do you know that there's a difference between the assignment operator= and the comparison operator==?

    By the way I wouldn't use Vsky's method since it relies on a non-standard function like getch().


    Jim

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Quote Originally Posted by jimblumberg View Post
    By the way I wouldn't use Vsky's method since it relies on a non-standard function like getch().
    Jim
    I'm also begginer He can use scanf too instead of getch() it doesn't matter.

    Btw what's wrong with getch()
    Last edited by Vsky; 04-10-2015 at 01:28 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    He can use scanf too instead of getch() it doesn't matter.
    But it does matter. Using non-standard functions that were introduced by a long obsolete compiler is really not a good idea when learning to program. Stick with standard C functions until you are familiar with the language.

    Btw what's wrong with getch()
    It's a non-standard function that is only available with a few compilers and operating systems.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input 'Q' quit 'C' Continue
    By Aliano in forum C Programming
    Replies: 5
    Last Post: 10-28-2013, 10:50 PM
  2. Enter 'q' to quit. Help?
    By jigamuffin in forum C Programming
    Replies: 8
    Last Post: 03-19-2012, 08:10 AM
  3. <enter> to quit
    By s_UNI_ in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2005, 05:40 AM
  4. Hit <ENTER> to continue...
    By now5 in forum C Programming
    Replies: 2
    Last Post: 03-14-2004, 05:38 PM
  5. Press enter to continue
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 11:18 PM