Few questions from "Accelerated C++"

This is a discussion on Few questions from "Accelerated C++" within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm going through Accelerated C++ (great book by the way!) and I've written a simple program (similar to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    Few questions from "Accelerated C++"

    Hi all,

    I'm going through Accelerated C++ (great book by the way!) and I've written a simple program (similar to the padding program on Chapter 2) but there's a warning from gcc (with the options -Wall -pedantic) and cannot figure out how to solve it.

    The problem is that there is a comparison between signed and unsugned integers in the line:
    Code:
    if (r==pad+1 && c==pad+1) {
    I've tried defining pad as
    Code:
    string::size_type pad=0;
    instead of int, but that doesn't work. Can someone please explain why I get this warning message and how to avoid it?

    Here's my program:
    Code:
    #include<iostream>
    #include<string>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main()
    {
            cout << "Please enter your name: ";
            string name;
            cin >> name;
            const string greeting = "Hello " + name + "!";
    
            cout << "Please enter how much padding you'd like: ";
            int pad=0;
            cin >> pad;
    
            const int rows=2*pad+3;
            const string::size_type cols=greeting.size()+2*pad+2;
    
            for (int r=0; r!=rows; ++r) {
                    string::size_type c=0;
                    while (c!=cols) {
                            if (r==pad+1 && c==pad+1) {
                                    cout << greeting;
                                    c+=greeting.size();
                            }
                            else if (r==0 || r==rows-1 || c==0 || c==cols-1) {
                                    cout << "*";
                                    ++c;
                            }
                            else {
                                    cout << " ";
                                    ++c;
                            }
                    }
                    cout << endl;
            }
            return 0;
    }
    Thanks

    Spiros

    ps I'm sure I'll have plenty more questions in the near future so I'll keep posting them on this thread instead of creating new threads etc..

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    Even if you declare 'pad' as 'string::size_type' 'r' would still be an 'int'.

    Soma

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    An int can be both positive and negative. An unsigned int (or string::size_type) can contain only positive values. Their ranges don't overlap, hence the warning.

    Basically you'll need to make all the int's unsigned (or string::size_type). Neither the number of rows, columns or paddings being negative wouldn't make any sense.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    Thanks, the idea of overlapping ranges makes sense. I tried using long int's so that they become unsigned, instead of int's but that didn't work. In the book the declerations are:
    Code:
    int r (inside the for loop)
    const int pad
    string::size_type c
    so I don't understand why the c==pad+1 works in the book since c is unsigned and pad is signed.
    Last edited by s_siouris; 04-11-2008 at 11:37 AM. Reason: silly spelling error

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    The book might just be ignoring the warning.

    If you want your int to be unsigned, use unsigned int. Using long int just makes the type be potentially able to hold more values, it doesn't change whether it is signed. You can have unsigned long or signed long as well.


    >> I'll keep posting them on this thread instead of creating new threads etc.
    If they're not related to this question, feel free to post a new thread. It's easier for new topics to get full answers than posting a bunch of different questions on one thread.
    Last edited by Daved; 04-11-2008 at 11:57 AM. Reason: Yeah, I meant warning.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,763
    It's better to compare types of the same sizes (eg, compare size_t to size_t), to avoid unexpected bitebacks.
    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.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Daved View Post
    The book might just be ignoring the error.
    I don't think it's an error. A good compiler will warn, though.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    cheers, that helped a lot. I thought that long int's where unsigned, but it seems I read the manual wrong. I changed the variables in question to unsigned int's and there's no problem at all now.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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