Thread: Can't find why this isn't working...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Can't find why this isn't working...

    Well I just started C++ and I can't figure out where I'm messing up this program... It should be giving me the maximum number but it is not. I think it's in the for loop I'm having a problem but I'm not sure. This is what I did so far. Could anyone point me in the right direction to get this runnning properly?
    Thanks.

    Code:
    #include <iostream>
    
    using std:: cout;
    using std:: cin;
    using std:: endl;
    
    double larger (double x, double y);
    
    int main ()
    {
    	int number1;
    	int number2;
    	int number3;
    	int number4;
    	int number5;
    	int number6;
    	int number7;
    	int number8;
    	int number9;
    	int number10;
    	int number11;
    	int number12;
    	int number13;
    	int number14;
    	int number15;
    
    	cout<<"Enter 15 numbers: ";
    	cin>>number1>>number2>>number3>>number4>>number5>>number6>>number7>>number8>>number9>>number10>>number11>>number12>>
    		number13>>number14>>number15;
    	cout<<"The largest number is: ";
        cin>>larger ( <<endl;
    	return 0;
    }
    
    double larger (double x, double y)
    {
    	For int counter = 1; counter<=14, counter++){
    		double max = x
    			if (y > max)
    				max = y
    				return max;
    	}
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You might enjoy a simple C++ constrol structures tutorial.
    You might also like another C++ tutorial on arrays too.
    This way, you don't need num1, num2, num3 variables.

    Then, try compiling it, see what errors you get, and see if you can resolve them. If impossible, then ask back here and tell us the errors you got. But don't just dump a little project on us and just say fix it

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    For int counter = 1; counter<=14, counter++){
    
    should be:
    
    for(int counter = 1; counter<=14; counter++) {
    Code:
    For int counter = 1; counter<=14, counter++){
    
    should be:
    
    for(int counter = 1; counter<=14; counter++) {
    Code:
    using std:: cout;
    using std:: cin;
    using std:: endl;
    
    should be (just for general consistancy):
    
    using std::cout;
    using std::cin;
    using std::endl;
    Code:
        cin>>larger ( <<endl;
    
    should be:
    
        cin>>larger (ARGUMENT HERE, ARGUMENT HERE) <<endl;
    
    your larger function is suppose to take 2 arguments.. so you must put something there, or change the larger function.
    Thats it for the syntax errors, now the concepts in your program.. The way you are collecting the numbers is fine, but you have to pass the numbers to your 'larger' function to sort, but you arent doing that, so it will not work. You would pass them by putting them in an array, ie. number[100], number[1] = this, number[2] = this. Also doubt the way you sort inside your for statement in your 'larger' function will work.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Why dont you use an array instead of using 15 inter declarations?


    Code:
    int number[15] = { 0 };
    int max = 0;
    
    while( ctr < 15 ) {
            cin >> number[ctr];
            
    if( max < number[ctr] )
      max = number[ctr];
    
    ctr++
    }
    
    print max value here

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well one, this line.

    Code:
    cin>>larger ( <<endl;
    It is missing the ")".
    But mostly it is a function that returns a value when you pass two vvariables to it. You are not passing two variables, you are not even doing anything with what it returns.

    Also, this:
    Code:
    For int counter = 1; counter<=14, counter++)
    You left off a "("...right after the "for".

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That code doesn't compile. If you look at it as pseudo code, it is not correct because the for loop looks like it expects an array, but the rest of the larger function looks like it calculates the larger of two doubles.

    If you know arrays or vectors, you should make one that holds 15 numbers. You should then change your larger function to take that array or vector as a parameter and go through the for loop looking at each value to see if it is more than the max like the code is doing with x and y.

    If you don't know arrays, you will need the for loop to be inside main(), not larger(). You would have only one number variable and one max variable, and reuse the number variable each time to see if it is larger than the current max.

    You still have a ways to go on this.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Tonto thanks for the tutorial, it will certainly help. Haven't gone through arrays in C++ yet, so I have to do without it... and I asked to be pointed in the right direction because I couldn't see what I not doing right .

    Dae thanks for correcting the consistancy of my programming.

    Enahs Ok I see what you're saying and that's where this is not working properly. I have to work on passing the variables to the larger function...

    Thank you so much now I know where to direct my attention to fix this.
    If I have more problems, I'll let you know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux find utilty
    By vaibhavs17 in forum Tech Board
    Replies: 5
    Last Post: 05-12-2009, 04:40 AM
  2. Working on huge projects
    By h3ro in forum Tech Board
    Replies: 2
    Last Post: 03-20-2009, 09:00 AM
  3. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  4. Working with bits and SHA-1
    By Desolation in forum C++ Programming
    Replies: 6
    Last Post: 12-28-2008, 01:34 AM
  5. working out...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-10-2003, 10:20 AM