What's wrong with this program, and how to fix it?

This is a discussion on What's wrong with this program, and how to fix it? within the C++ Programming forums, part of the General Programming Boards category; Hi, When I compile this program, there's always a message on the screen as below: "exer3.cpp: In function ‘int main()’: ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    What's wrong with this program, and how to fix it?

    Hi,

    When I compile this program, there's always a message on the screen as below:

    "exer3.cpp: In function ‘int main()’:
    exer3.cpp:13: warning: comparison between signed and unsigned integer expressions
    exer3.cpp:18: warning: comparison between signed and unsigned integer expressions
    exer3.cpp:22: warning: comparison between signed and unsigned integer expressions
    exer3.cpp:27: warning: comparison between signed and unsigned integer expressions
    exer3.cpp:35: warning: comparison between si "

    Despite this, I am able to run the program and have the expected results. However, I don't know how to fix the areas of the program which the warnings point to. Could you please help? Thanks a lot!

    Code:
    // Exercise 3/Chapter 3: Programming: Principles and Practice using C++
    
    #include "std_lib_facilities.h"
    
    int main()
    {
    	vector <double> myvector;
    	double number = 0;
    	while (cin >> number)
    		myvector.push_back(number);
    	
    	double sum = 0;
    	int i;
    	for (i = 0; i < myvector.size(); ++i)
    		sum += myvector[i];
    	
    	// compute distance
    	vector <double> mydistance;
    	for (i = 0; i < (myvector.size()-1); ++i)
    		mydistance.push_back(myvector[i+1]- myvector[i]);
    	
    	cout << "Numbers: " << endl;
    	for (i = 0; i < myvector.size()-1; ++i) {
    		cout << " " << myvector[i];
    	}
    	
    	cout << "\nDistance: " << endl;
    	for (i = 0; i < mydistance.size(); ++i) {
    		cout <<" " << mydistance[i];	
    	}
    	
    	// find min, max
    	double max, min;
    	max = min = myvector[0];
    	cout << '\n';
    	for (i = 0; i < myvector.size(); ++i) {
    		if (myvector[i] > max) {
    			max = myvector[i];
    			cout << "Max = " << max << "\tMin = " << min << endl;
    			min = max;
    		}
    		else if (myvector[i] < max) {
    			min = myvector[i];
    			cout << "Max = " << max << "\tMin = " << min << endl;
    			max = min;
    		}
    		
    	}
    	
    }

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    This is common with the STL; they use unsigned integers as indices; most folks (myself included) tend to just say "int X" out of habit which is a signed integer. Change int i; to unsigned i; above and see if that doesn't help.


    Peace
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    myvector.size() returns an unsigned integer type, while the loop counter is signed. One way to get rid of the warnings is to use an appropriate type, for example vector<double>::size_type (which is the type that size() returns in this case). Or you could just make the loop counter an unsigned int.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by pantera
    What's wrong with this program, and how to fix it?
    It was written by a noob

    Those warning are typical when you declare a variable as signed and then compare it to an unsigned value. a variable of type int is signed. You have to explicitly declare it as unsigned int to change this.

    your integer i is signed, myvector.size() returns unsigned
    Last edited by abachler; 12-17-2009 at 05:56 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Thank you all so much for your time & help. I'm doing another program but get stuck... The program is about rewards for the inventor of chess-board. For 1st square, he's rewarded with 1 grain of rice, 2nd square for 2 grains, 3rd for 4 graints,... doubling grains for each of the 64 squares. The exercise asks me to identify how many squares are needed for at least 1,000 grains of rice, at least 1,000,000, and at least 1,000,000,000. I've tried writing the program for 1,000 grains. Where I got stuck is that, how can I write a program that accommodates all these tests (1,000, 1,000,000, etc...) in a single loop? I can't figure out how, and currently I'm just thinking of writing 3 loops for each of the 3 tests. Can you please suggest some hint? Thanks a lot.

    Code:
    // Exercise 8: a Game of Chess reward
    #include "std_lib_facilities.h"
    
    int main()
    {
    	const int N = 64;
    	int grain, sum;
    	
    	sum = grain = 1;
    	int i = 2;
    	int n;
    	while (i <= N) {
    		grain *= 2;
    		sum += grain;
    		if (sum >= 1000) {
    			n = i;
    			cout << n <<" squares are needed for 1000 grains\n";
    			cout <<"Sum of grains: " << sum << endl;
    			break;
    		}
    
    		++i;
              }
    }

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Sorry for this silly Q. After thinking for a while, I figured out that I'll need to write a function that takes an argument: 1000, 1000000, etc...

    Code:
    // Exercise 8: a Game of Chess reward - version 2
    #include "std_lib_facilities.h"
    
    int find_squares(int); 
    
    int main()
    {
    	int num_of_grains = 0;
    	cout <<"Pls enter the number of grains: ";
    	cin >> num_of_grains;
    	if (num_of_grains <= 0)
    		cout <<":=), you don't need any grains - you even want to donate grains! :-)";
    	else
    		find_squares(num_of_grains);
    }
    
    int find_squares(int num_of_grains) // a function to find the number of squares
    						      // needed to get a specified number of grains of rice
    						    
    {
    	int grain, sum_of_grains, square;
    	const int N = 64; // total number of squares on a chessboard
    	sum_of_grains = grain = 1;
    	square = 2;
    	while (square <= N) {
    		grain *= 2;
    		sum_of_grains += grain;
    		if (sum_of_grains >= num_of_grains) {
    			cout << square << " are needed for " << num_of_grains <<" of grains of rice\n";
    			break;
    		}
    		++square;
    	}
    }
    Last edited by pantera; 12-19-2009 at 11:25 PM.

Popular pages Recent additions subscribe to a feed

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