Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › having trouble terminating a loop when a button is hit
New Posts  All Forums:Forum Nav:

having trouble terminating a loop when a button is hit

post #1 of 5
Thread Starter 
Hello people,

I've got a small question. How do I terminate a loop until scanf reads in '$' from command line.
Every time I run it, it just continues forever.
I'm sorry if it's a very simple mistake very new to C.

Code:
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

char getCmd(void);

int main(void) {
   char c = getCmd();
   if (c != '\\0')
      printf("%c\
", c);
   else
      printf("Incorrect command\
");
   return EXIT_SUCCESS;
}

char getCmd(void) {
   
   char input[MAX];
   int i;
   
   printf("? ");
   while(input[0] != '$') {
      
      for(i=0; i<MAX; i++) {
         scanf("%c", &input[i]);
          if(input[i] ==  '\
') {
           break;
          }
      }

      if( i>1 ) {
         input[0] = '\\0';
      }
   }
   
   return(input[0]);
}
the end result should be something like this:

./program
? 2
2
? b
b
? cc
Incorrect command
? zzzzzzzzzzzzzzzzzzzzz
Incorrect command
? $

Thank you
Edited by Gurellaz696 - 5/12/11 at 9:00am
Fragger v2.0
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600K Z68X-UD3H-B3 Gigabyte 7950 Corsair  
RAMHard DriveHard DriveOptical Drive
Corsair  Seagate Seagate LG 
CoolingOSMonitorKeyboard
Corsair H60 Windows 7 Ultimate 64 Bit LG E2441 Logitech G110 
PowerCaseMouse
Corsair HX650 Fractal Design Define XL Microsoft X6 
  hide details  
Reply
Fragger v2.0
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600K Z68X-UD3H-B3 Gigabyte 7950 Corsair  
RAMHard DriveHard DriveOptical Drive
Corsair  Seagate Seagate LG 
CoolingOSMonitorKeyboard
Corsair H60 Windows 7 Ultimate 64 Bit LG E2441 Logitech G110 
PowerCaseMouse
Corsair HX650 Fractal Design Define XL Microsoft X6 
  hide details  
Reply
post #2 of 5
if(i>1 || '\
'){
input[0] = '\\0';
}

i>1 || '\
' will always return true, so you will set input[0] = '\\0' every run through the loop, no matter what the input was.

I'm not really sure what you intended with that conditional, though, so I can't offer a fix.
Jillian
(11 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K MSI P67A-G45 B3 MSI GeForce GTX560 Ti GSKILL F3-10666CL9D-8GBRL 
CoolingOSKeyboardPower
Cooler Master Hyper 212 Plus Windows 7 Professional 64-bit Razer Lycosa Corsair 520HX 
CaseMouse
Cooler Master Stacker 830 Razer Mamba 
  hide details  
Reply
Jillian
(11 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K MSI P67A-G45 B3 MSI GeForce GTX560 Ti GSKILL F3-10666CL9D-8GBRL 
CoolingOSKeyboardPower
Cooler Master Hyper 212 Plus Windows 7 Professional 64-bit Razer Lycosa Corsair 520HX 
CaseMouse
Cooler Master Stacker 830 Razer Mamba 
  hide details  
Reply
post #3 of 5
Thread Starter 
oh damn sorry that isn't meant to be there
it's just meant to read
if(i>1){
input[0] = '\\0';
}

but in general i just want to make a loop so it keeps on printing out:
./program
? 2
2
? b
b
? cc
Incorrect command
? zzzzzzzzzzzzzzzzzzzzz
Incorrect command
? $
Fragger v2.0
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600K Z68X-UD3H-B3 Gigabyte 7950 Corsair  
RAMHard DriveHard DriveOptical Drive
Corsair  Seagate Seagate LG 
CoolingOSMonitorKeyboard
Corsair H60 Windows 7 Ultimate 64 Bit LG E2441 Logitech G110 
PowerCaseMouse
Corsair HX650 Fractal Design Define XL Microsoft X6 
  hide details  
Reply
Fragger v2.0
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600K Z68X-UD3H-B3 Gigabyte 7950 Corsair  
RAMHard DriveHard DriveOptical Drive
Corsair  Seagate Seagate LG 
CoolingOSMonitorKeyboard
Corsair H60 Windows 7 Ultimate 64 Bit LG E2441 Logitech G110 
PowerCaseMouse
Corsair HX650 Fractal Design Define XL Microsoft X6 
  hide details  
Reply
post #4 of 5
Please wrap your code with code brackets:
[ code ]
[ / code ]

Like above, with no spaces though. It'll make it easier to read your code and help you.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #5 of 5
Thread Starter 
Quote:
Originally Posted by lordikon View Post
Please wrap your code with code brackets:
[ code ]
[ / code ]

Like above, with no spaces though. It'll make it easier to read your code and help you.
thank you
your right it looks much more better now
Fragger v2.0
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600K Z68X-UD3H-B3 Gigabyte 7950 Corsair  
RAMHard DriveHard DriveOptical Drive
Corsair  Seagate Seagate LG 
CoolingOSMonitorKeyboard
Corsair H60 Windows 7 Ultimate 64 Bit LG E2441 Logitech G110 
PowerCaseMouse
Corsair HX650 Fractal Design Define XL Microsoft X6 
  hide details  
Reply
Fragger v2.0
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600K Z68X-UD3H-B3 Gigabyte 7950 Corsair  
RAMHard DriveHard DriveOptical Drive
Corsair  Seagate Seagate LG 
CoolingOSMonitorKeyboard
Corsair H60 Windows 7 Ultimate 64 Bit LG E2441 Logitech G110 
PowerCaseMouse
Corsair HX650 Fractal Design Define XL Microsoft X6 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › having trouble terminating a loop when a button is hit