DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31

Thread: New to C++

  1. #16
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    Quote Originally Posted by Razee Marikar
    32 bit / 256MB of memory is too small to consider.
    But I like always "consider", I belive this is better always ...

    http://en.wikipedia.org/wiki/Boolean_datatype#C.2B.2B
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  2. #17
    Join Date
    Nov 2007
    Posts
    2

    urgent

    what is another way of writing these statements using printf function :

    cout << "\nInsert a value between 1 and " << MAX_RANGE << " : ";
    cin >> input;
    cout << "Value is greater than " << input << ". Try again : ";
    cout << "Value is less than " << input << ". Try again : ";
    cout << "That's right! Value was " << input;
    cout << "\nYou have needed " << counter <<" attempts.";

    I need urgent help
    Last edited by gordon87; 11-24-2007 at 08:14 AM. Reason: Incomplete request

  3. #18
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    First why not openeing your own thread ...
    second does the given method work originally to look for an alternative ?

    if you just want to know what's missing, you need an if condition to compare the input ... and a while loop to count attempts and exist only when the user input a valid number ...
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #19
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    printf("\nInsert a value between 1 and %d: ", MAX_RANGE);
    scanf("%d", &input);
    printf("Value is greater than %d. Try again: ", input);
    printf("Value is less than %d. Try again: ", input);
    printf("That's right! Value was %d", input);
    printf("\nYou have needed %d attempts.", counter);

    That's assuming input and counter are integer variables and MAX_RANGE is a literal / const int.

  5. #20
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by Amahdy
    while(true) >> needs one bit for comparison 0 or 1 [finally 8 bit to complete a word but the memory will work only with the last bit]
    while(1) >> 1 is considered as integer, it will occupie 32 bit for comparison only ... I don't know if the code optimizer in C++ compiler will store the 1 as a bit containig "1" or no ; I think no ; so it's better to use the reserved true and false isn't it ?
    It will NEVER store it as one bit because there's no built-in type that represents a bit. The closest you can get is bit-fields but the fields are actually packed in larger, addressable datatypes. So 'true' and 'false' are always represented as 8 bits, or more.
    Danny Kalev

  6. #21
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    Quote Originally Posted by Danny
    It will NEVER store it as one bit because there's no built-in type that represents a bit. The closest you can get is bit-fields but the fields are actually packed in larger, addressable datatypes. So 'true' and 'false' are always represented as 8 bits, or more.
    yes that's what I said, so it's 8bit and always better than 32 bit .
    finally while(true) shorter than while(1)
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  7. #22
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Sometimes, a word aligning compiler will actually use up 32 bits, by aligning the next word such that it starts at a word address. And for small programs, it doesn't really matter. In a small program, nothing other than readability is affected. And an optimizing compiler will even do away with the condition whether it is while(1) or while(true) and just make a jump instruction. So it doesn't affect anything other than readability.

  8. #23
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    one of the formal expressions for the while loop is :
    while(IntegerExpression | BooleanExpression | ...)
    so while(true) will be classified as while(BooleanExpression) and while(1) as IntegerExpression , and depending on the compiler and the target machine, it will decide how to deal with each of them , the link I sent before tell that C compiler work with the true and false keywords as 32 bit, but for new C++, it differentiate between two case of BooleanExpression , the simple one and the coposite one ... like the BooleanExpression "IntegerExpresion BooleanOperator IntegerExpression" eg "3>5" ; and the direct using of true and false, the latter will be compiled into 8 bits only now.
    so if this is a small asignment or whatever, It's better to be famillier with the best optimized coding for not seeing such application like IE7 taking in my CPU 110,000K of memory when realplayer for example is just taking 500k !!
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  9. #24
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    IE7 and RealPlayer are too far fetched for a comparison. That would make a whole new thread and about 100 posts. As for while(true) and while(1), for an old C++ compiler, it is the same. New C++ compilers optimize the code (unless you tell it not to). And then, while(<constant>) expression is totally eliminated according to its truth value. If <constant> evaluates to non zero, a simple unconditional jump is made at the end of the code. If it evaluates to zero, all of the code may be optimized away (removed) and you will get a warning (if enabled) saying that the code is unreachable. So, basically, with an optimizing compiler, while (true) and while (1) are the same, 0 sized code. :-)

  10. #25
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Yes should not make a comparison between multimedia program and web browser, but between IE7 and firefox we can : I have now 1 opened tab in each of them, both pointes to this forum page and .. IE7 188,xxxK ; firefox2 23,xxxK thankyou FireFox :)

    in post #10 I said :
    ...
    I don't know if the code optimizer in C++ compiler will store the 1 as a bit containig "1" or no ; I think no ; so it's better to use the reserved true and false isn't it ?
    I still think "no" it doesn't, because it's not sure if really the user wants that or no, in compiling phases the compiler doesn't calculate the program arithmatics and decide by itself if it should replace tokens by others, finally I'm not sure maybe they have found a good method to let it do that with constants ...
    for example :
    while(7-7) will the compiler calculate 7-7, and transform it to while(0) then to while(false)
    and for while(false & true) what will the compiler do ? it's the c++ language semmatics not the compiler's which just map the written language to a mchine language.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  11. #26
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Quote Originally Posted by Amahdy
    I still think "no" it doesn't, because it's not sure if really the user wants that or no, in compiling phases the compiler doesn't calculate the program arithmatics and decide by itself if it should replace tokens by others, finally I'm not sure maybe they have found a good method to let it do that with constants ...
    The code optimizer will not store 'true' as a bit containing one either. It will still be one whole byte, and may take up one full word (memory space, not while comparison / loading) if word alignment is being done.

    Code optimization which usually runs after compilation, and before machine language is made. Ie it is run on an intermediate code. And regardless of whether it is while(1) or while(true), it is optimized away. No code is generated except a jump at the end. Using a bool and int variables is a different issue altogether.

    Here is one reference. Look up Complex Branch Optimization (page 9) http://www.windriver.com/products/de...zations_wp.pdf

  12. #27
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9
    :eek:

    Ok, new question. I have moved onto a bit of GUI programming in Visual C++ and I'm making a sort of shop program for my friends. Basically it has a login for and when the correct password is entered it shows all the fields(they are hidden). This works fine. It has a list of checkboxes where the user can select the item, and at the bottom it has a field for "Name" and "CG" and a button to send their order and info to an email as well as a total price textbox and an update button.

    How can I have the update button go through and see what is check and then add $xx to the total? Or maybe have it so when the user clicks the check box it auto adds to the total.

    Also, what would be the best way to send the information to an email? Just collect everything that is entered/checked and email it?

    Thanks,
    alien13(Nathan)
    Last edited by alien13; 11-29-2007 at 07:14 AM.

  13. #28
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    Quote Originally Posted by alien13
    How can I have the update button go through and see what is check and then add $xx to the total? Or maybe have it so when the user clicks the check box it auto adds to the total.
    in th click event of the update button, see if the checkboxes item by item is checked, for each checked item add the proper quantity in a variable and finally add this quantity to the old one found at the previous total textbox ..
    or maybe .. track the event ::OnClick for the checkbox and just add or substract from the existing value depending whether the user check or uncheck .

    Quote Originally Posted by alien13
    Also, what would be the best way to send the information to an email? Just collect everything that is entered/checked and email it?
    you maybe need to generate a good readable email form, for example save in a string :
    "From: " + emailFrom + "\n" + "checkbox1 value is: " + check1 + ...
    of course change checkbox1 value by a propper desciption, I think sending this form will be better.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  14. #29
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    Quote Originally Posted by Razee Marikar
    The code optimizer will not store 'true' as a bit containing one either. It will still be one whole byte, and may take up one full word (memory space, not while comparison / loading) if word alignment is being done.

    Code optimization which usually runs after compilation, and before machine language is made. Ie it is run on an intermediate code. And regardless of whether it is while(1) or while(true), it is optimized away. No code is generated except a jump at the end. Using a bool and int variables is a different issue altogether.

    Here is one reference. Look up Complex Branch Optimization (page 9) http://www.windriver.com/products/de...zations_wp.pdf
    well if here he is talking about his product and how he can optimize the code using it, finally most of his used methods is changing everything to inline code ... this bring a faster code but of course more memory consumer...
    now just I want to point to the example listed in page 9 as you have said .. well it's already known that a while loop in run-time is a nested sequence of if conditions so ...
    if B is the boolean expression, X is the loop body ,Y is the rest of code; and the general form is :
    while B do X ; Y;
    this is equivalent exactly to:
    if B do (while B do X) else Y ;
    this is somehow what he was trying to do, but just he make extra calculations for example if the loop iterate 3 times the code will be instead :
    X ; X ; X ; Y ;

    now have to tell two problems in this method, the first is easy to solve and the second is never solvable by this method :
    first:
    x=6; while (x-=3) a=1; if(x==0) "can you come here ?";
    and the obvious solution is to not remove the if condition and to set the x variable to 0 before removing the while loop, it will become more complicated if we depend on x inside the loop body.
    second:
    while(true) wait_for_some_thing();
    if we follow his method he will die writing X;X;X;.. because nobody know how to exit from this loop.. so if iside this documentation he is just providing some techniques I suppose they aren't the perfect techniques ... and they aren't what the user want all the time as he explained himself at the end: this is something bas from the programmer to write such code as the example in page 9 .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  15. #30
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    I wasn't referring to the whole optimization stuff. That is a very long subject. Just what happens when there is a CONSTANT expression inside the while. Like while(1) or while(ture), not while(a==1) or while(a==true). That is a different issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center