running code, goes on forever, without stopping......

How about this post, my last try failed to post so here it is again.

this gets to 4 I think, can't really tell, cause its so fast and just keeps repeating....



// function counts down from higher number to lower number entered
// e.g., if numbers entered are 4 and 8
// output is: 8 7 6 5 4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <string>
#include <iostream>
#include <ctime>


using namespace std;

/*
 * main entry point
 */
int main(int argc, char** argv) {

    void countDown(int, int);
    int high, low;
    cout << "I will count down from the higher number you enter " <<
 
   for(x = high; x = low; --x)  
     cout << x << " " << endl;

    
    return 0;
}//end main.......... 
Last edited on
Line 30, try
for (x == high; x <= low; --x)
Last edited on
It should be x>=low instead of x=low. And the one of danny is wrong.
You are also missing a couple braces, and can't declare a function inside main like you are.
So the only thing in mine that's wrong is x<=low which should be x>=low?
Your for loop looks a bit suspect. The first expression is supposed to be an initialization, but you have an conditional statement there. So it accomplishes nothing. I imagine you meant to use a single =, instead of ==?
So it's completely wrong? Wow, don't I feel stupid.
Ok, I fixed the x>=low and it worked out fine, but does that mean its still wrong by not declaring a function inside main............
Yep.
turbowhat wrote:
Ok, I fixed the x>=low and it worked out fine, but does that mean its still wrong by not declaring a function inside main............

Functions should be declared before main not within it.

Edit: You still need to fix this:
1
2
3
4
    if (high < low) {
        low = high;
        high = low;
    }


Notice that it does not actually swap the values. You need to use an intermediate variable.
Last edited on
Alright I give, I'm confused......
When you do
1
2
low = high;
high = low;

the values aren't swapped.

Consider when low is 10, and high is 2, for example.
The first line sets low to be equal to high, which is 2.
Then the second line sets high to be equal to low, which is 2.
So the values aren't swapped.
so low = low; and high = high;

got that, I'm more confused on the function declaration part, what part of my code did I screw up?
Line 14, remove it.

Actually, place it before main:

1
2
3
4
5
6
7
8
9
10
11
12
13
int function (int, int);

int main
{
    //...
    //...
}

int function (int a, int b)
{
    /*...
    ...*/
}
Last edited on
Line 14, remove it.

No, move it before main()

1
2
3
4
5
6
...
void countDown(int, int);

int main(int argc, char** argv) {
    int high, low;
...


You can't have your implementation inside of main either, move that after main and make sure the lines it contains are enclosed in brackets.
Last edited on
Yeah, I realized that just after I posted it, so I tried to correct it, sorry about that.
thats all it was? I moved that............I think I had that like 2 hours ago and I thought that threw me an error, sorry for bugging out, I just swear it through me an error so I wasn't going to do it again..........

Thank you for your patience....
Topic archived. No new replies allowed.