why dose this give me

This is a discussion on why dose this give me within the C++ Programming forums, part of the General Programming Boards category; Code: deleted why dose this only give me: ouput is: not Output is: <somthing here>...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    why dose this give me

    Code:
    deleted
    why dose this only give me:
    ouput is:
    not
    Output is: <somthing here>
    Last edited by IM back!; 09-05-2008 at 11:40 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You never change output[0], since runoften is at least 1 before you get to anywhere in your "input parsing routine" -- and if output[0] happens to be \0, then your string ends before it even gets started.

    (I put "input parsing routine" in quotes because it's not clear that you mean to be skipping all those letters -- when runoften increases two or three or four at a time, you're going to be skipping two or three or four input characters. And I also really hope you have indentation in your source file, or else eeesh.)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And:
    Code:
    runoften=runoften;
    is absolutely meaningless. Assigning a variable to itself is NEVER meaningfull [1]


    [1] If it's a pointer to a hardware register then it may have a meaning (e.g. writing bits that are set will actually clear those bits in some hardware registers).

    And would it not make more sense to have a function that produces N stars followed by the symbol X?

    Further, instead of having a huge number of if-statements, use a switch-case [or at least use if-else-if chains, since if the letter is 'a', then it's obviously NOT going to be equal to 'b'.

    --
    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.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Code:
    int aa;
    int bb;
    string name;
    char input[1000];
    char output[1000];
    
    void a
    {
     while (aa != 100)
     {
      cout<<"loop";
      aa=aa+1;
      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;    
       }
      }
     }
    cout<<output;
    }
    Last edited by IM back!; 09-05-2008 at 11:39 AM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    if(input[aa]='a'|'d'|'g'|'j'|'m'|'p'|'s'|'w')
    I only read that dubious line and didn't even bother going further ahead. You need that to be broken into multiple conditions. Or'ing those together is not at all doing what you want.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    input[aa]='a'|'d'|'g'|'j'|'m'|'p'|'s'|'w'
    Well, 'a'|'d'|'g'|'j'|'m'|'p'|'s'|'w' is some large number, you then assign it to input[aa] (not comparison, mind you, but assignment), and then output[1] is set to *.
    (And continues etc.)

    Then when you go to print output, it starts at output[0], finds that that is the null character, and stops (because that's how strings work -- they go until you get a null character and then you stop).

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    if(input[aa]='a'|'d'|'g'|'j'|'m'|'p'|'s'|'w')
    Are those supposed to be ORs? If so, that's not how it works.
    Code:
    if (input[aa] == 'a' || input[aa] == 'd' || input[aa] == 'g' ...

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    OK thank you all, I still have a lot to learn , I made some corrections. now how do I get the to print the output

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fantastic! I am glad you are at least halfway getting it now. Next time don't be so ashamed by your initial post that you delete the code, however. Believe it or not, others will probably one day ask the same question and benefit from seeing someone who asked it in a similar way. Right?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by IM back! View Post
    OK thank you all, I still have a lot to learn , I made some corrections. now how do I get the to print the output
    You build the output appropriately. (At least all the code you've posted has had inappropriately created output, with \0's at the *beginning* of the output, which is bad. You need to start writing into your output at character 0.)

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    OK thanks now it works i simply defined aa like this int aa = -1;

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wow... I didn't even notice that it was uninitialized. You should put those as local variables, by the way. There is no earthly reason for them to exist outside of your function.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by master5001 View Post
    Wow... I didn't even notice that it was uninitialized.
    That's because they are initialized (all global variables are initialized to 0 unless otherwise explicitly initialized). I agree that that's not a good reason to make them global.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    > if (input[aa] == 'a' || input[aa] == 'd' || input[aa] == 'g' ...
    To improve further, consider
    if ( strchr( "adg", input[aa] ) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by tabstop View Post
    I agree that that's not a good reason to make them global.
    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.

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21