Thread: Unknown Problem

  1. #1
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52

    Unknown Problem

    Ok guys i got a problem and i dont know where to start to fix it...
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    void display();
    int getinput();
    int Subgetinput(char* p, int size);
    bool Subgetinput2(char* p);
    
    char input; // YEAH SO ITS GLOBAL....SO WHAT
    
    int main()
    {
        //char aWord[5];
        
        while(true)
        {
                   system("cls");
                   printf("\n\t\tMain Menu\n");
                   printf("F1 = Get Input\nF2 = Display input\nF3 = Exit\n");
                   char choice;
                   choice = getch();
                   if(choice == 0)
                   {
                             choice = getch();
                             if(choice == 61) break;
                             
                             switch(choice)
                             {
                                           case 59:
                                                getinput();
                                                break;
                                           case 60:
                                                display();
                                                break;
                                           default:
                                                printf("try again.");
                                                break;
                             }
                   }
                   else printf("Not a valid choice.");
                   
            }
    }
                   
    int getinput()
    {
        
        char arr1[15];
        printf("enter Some characters: ");
        
        int over;
        over = Subgetinput(arr1, sizeof(arr1));
        //input = ;
        system("pause");
    }
    
    int Subgetinput(char* p, int size)
    {
        char* end;
        end = p + size - 1;
        char in;
        int overflow;
        overflow = 0;
        
        while(true)
        {
                   in = fgetchar();
                   input = in;
                   if(in == 10) break;
                   if(p < end)
                   {
                        *p = in;
                        p++;
                   }
                   else overflow++;
                   
        }
        
        *p = 0;
        return overflow;
    }
    
    bool Subgetinput2(char* p)
    {
        char* beginning = p;
         
         if(*p == 0) return false;
         
         while(*p != 0)
         {
                  if(*p ==' ')return false;
                  p++;
         }
         
         p = beginning;
         
         while(*p != 0)
         {
                  if(*p==*(p + 1))
                  {
                             if(*p == *(p + 2)) return false;
                  }
                  
                  p++;
         }
         return true;
    }
    void display()
    {   
        
        printf("\t%s",input);
        
        system("pause");
    }
    its when you try to input somthing and then try to display it...it just blows up...i know its somthing dumb cause i tried (very badly i might add) to put two programs together very quickly...so if you guys can figure out whats wrong let me know.. Thanks

  2. #2
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    I've added some comments to your code. Look for comments that start with "TC" (for "The Colonial" )
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    void display();
    int getinput();
    int Subgetinput(char* p, int size);
    bool Subgetinput2(char* p);
    
    // TC: you've only got enough room for one character in this
    // variable, but you want to be able to read in multiple characters,
    // change this so that it can store more than one character.
    char input; // YEAH SO ITS GLOBAL....SO WHAT
    
    int main()
    {
        //char aWord[5];
        
        while(true)
        {
                   system("cls");
                   printf("\n\t\tMain Menu\n");
                   printf("F1 = Get Input\nF2 = Display input\nF3 = Exit\n");
                   char choice;
                   choice = getch();
                   if(choice == 0)
                   {
                             choice = getch();
                             if(choice == 61) break;
                             
                             switch(choice)
                             {
                                           case 59:
                                                getinput();
                                                break;
                                           case 60:
                                                display();
                                                break;
                                           default:
                                                printf("try again.");
                                                break;
                             }
                   }
                   else printf("Not a valid choice.");
                   
            }
        // TC: main() returns an int
        return EXIT_SUCCESS;
    }
                   
    int getinput()
    {
        
        char arr1[15];
        printf("enter Some characters: ");
        
        int over;
        over = Subgetinput(arr1, sizeof(arr1));
        //input = ;
        system("pause");
    
        // TC: You need to return ... something!
        return over;
    }
    
    // TC: This function needs to be looked at. you'll find that
    // this is probably the biggest point of pain for you at the
    // moment. Ask yourself what you're trying to do, and perhaps
    // rethink/recode this bit.
    int Subgetinput(char* p, int size)
    {
        char* end;
        end = p + size - 1;
        char in;
        int overflow;
        overflow = 0;
        
        while(true)
        {
                   in = fgetchar();
                   input = in;
                   if(in == 10) break;
                   if(p < end)
                   {
                        *p = in;
                        p++;
                   }
                   else overflow++;
                   
        }
        
        *p = 0;
        return overflow;
    }
    
    bool Subgetinput2(char* p)
    {
        char* beginning = p;
         
         if(*p == 0) return false;
         
         while(*p != 0)
         {
                  if(*p ==' ')return false;
                  p++;
         }
         
         p = beginning;
         
         while(*p != 0)
         {
                  if(*p==*(p + 1))
                  {
                             if(*p == *(p + 2)) return false;
                  }
                  
                  p++;
         }
         return true;
    }
    void display()
    {   
        // TC: "input" is a char, and you're trying to output a string,
        // either change this so that it outputs a single char (%c), or
        // change the input variable to a c-style string.
        printf("\t%s",input);
        
        system("pause");
    }
    BTW, this code is very "C like" and not really "C++ like". Is this intentional?

  3. #3
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    *Fixed it*
    Last edited by AndyBomstad; 05-09-2005 at 03:18 AM. Reason: fixed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Unknown problem
    By Mindphuck in forum C Programming
    Replies: 3
    Last Post: 04-08-2006, 02:35 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Unknown problem
    By pink_langouste in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 12:36 AM