Thread: why dose this give me

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by IM back! View Post
    thats because that is a part of a small app, I could have done it like this. operation(char input[100], int aa) but i find global variables easyer in this case.
    Really, that's not a good reason.
    If you have a good reason, use them. If you don't, then don't. It's bad practice.
    A small app is just not a good reason. Don't get into the habit of doing it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
     if(input[aa]=='a'||input[aa]=='d'||input[aa]=='g'||input[aa]=='j'||input[aa]=='m'||input[aa]=='p'||input[aa]=='s'||input[aa]=='w')
      {
       input[aa]='*';
      }
      else
      {
       if(input[aa]=='b'||input[aa]=='c'||input[aa]=='d'||input[aa]=='e'||input[aa]=='f'||input[aa]=='g'||input[aa]=='t'||input[aa]=='x')   {
        output[aa]='*';
        bb=aa;
        bb++;
        output[bb]='*';
        bb=0;
       }
       else
       {
        output[aa]='*';
        bb=aa;
        bb++;
        output[bb]='*';
        bb++;
        output[bb]='*';
        bb=0;
    Wouldn't it be much easier to read this way:
    Code:
    switch (input[aa])
      {
      case 'a':
      case 'd':
      case 'g':
      case 'j':
      case 'm':
      case 'p':
      case 's':
      case 'w':
        input[aa]='*';
        break;
      case 'b':
      case 'c':
      case 'd':
      case 'e':
      case 'f':
      case 'g':
      case 't':
      case 'x':
        output[aa]='*';
        bb=aa;
        bb++;
        output[bb]='*';
        bb=0;
        break;
      default:
        output[aa]='*';
        bb=aa;
        bb++;
        output[bb]='*';
        bb++;
        output[bb]='*';
        bb=0;  
      }
    I have no idea what your code is actually intending to do, so I have no idea if the above code is actually correct - it's just a replacement of your long if-statements with switch - which makes it easier to read. strchr() as suggested is also an option, but it's a bit of a roundabout way to do it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by IM back! View Post
    thats because that is a part of a small app, I could have done it like this. operation(char input[100], int aa) but i find global variables easyer in this case.

    now one last qusetion: why dose this not work:
    Code:
        
    if(input[aa]==' ')
         {
          output[bb]='0';
          bb++;
         }
    I also tryed:
    Code:
     if(input[aa]==32)
         {
          output[bb]='0';
          bb++;    
         }
    since 32 is the acsii code for "space" i think.
    Because it does work. You seem to have deleted all your original code, but I recall you using >> to read in your input, and it is not possible to read in a space into a string with >>.

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    I do indeed use cin>>input;, but if i do this:
    Code:
    cin>>input;
    cout<<input;
    it comes with spaces, so I dont understand what you mean, but ill try cin.getline( input, 100, '\n' )

    EDIT: it works with cin.getline( input, 100, '\n' ), thank you, i dont understand why thow.
    Last edited by IM back!; 09-07-2008 at 06:45 AM.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by IM back! View Post
    I do indeed use cin>>input;, but if i do this:
    Code:
    cin>>input;
    cout<<input;
    it comes with spaces, so I dont understand what you mean, but ill try cin.getline( input, 100, '\n' )

    EDIT: it works with cin.getline( input, 100, '\n' ), thank you, i dont understand why thow.
    If you have cin >> input and you type "hello world", input will only read "hello", and that's what will get printed. The next time through (if you have a loop) you'll get "world", but the space between them will be ignored.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please give ur point of view
    By clover in forum C Programming
    Replies: 2
    Last Post: 05-04-2004, 03:56 PM
  2. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  3. Can you give me your tip plz :)
    By dionys in forum C Programming
    Replies: 6
    Last Post: 04-11-2004, 11:14 PM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 02:09 PM