how to fix the syntax and logic errors ?

//Debug6-1
// 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
#include<iostream>
using namespace std;
int main()
{
void countDown(int lowest,int highest);
int highest,lowest;
cout <<"I will count down from the higher number you enter to the lower one " << endl;
cout <<"Enter a number" << endl;
cin >> highest;
cout <<" Enter another number " << endl;
cin >> lowest;
if(highest<lowest);
{
lowest=highest;
highest=lowest;
}
void countDown(int high,int low );
return 0;
}
void countDown(int lowest , int highest);
{
int x;
for(x=highest; x=lowest;--x)
cout << x << "" << endl;
}
1
2
3
4
5
if(highest<lowest);
{
lowest=highest;
highest=lowest;
}


remove the semicolon from the end of that if statement.

Now consider this situation, lowest is 10, highest is 5, therefore the code enters the for loop above, and lowest = highest; is executed, now lowest = 10 and highest = 10, you have destroyed the value originally stored in highest, you need to use a temporary variable to hold one of the values while you swap them around, or better still, use std::swap(highest,lowest);

There is some confusion in your code about how functions are used, the first line in main void countDown(int lowest,int highest); is a declaration of the function, and is correct.

The line before return 0; in main, is this void countDown(int high,int low ); and is incorrect, I think you're trying to call the function here, when really you are just declaring it again. To call a function you don't specify the return type, change that line to this countDown(high,low);

Finally, the function definition at the bottom has a semicolon after the parameter list, remove this, and now you should be good to go.

EDIT - To sum up, if statements, while statements, and function definitions do not end with semicolons, but that is a common mistake for beginners since most lines do. Also you should read up on how functions work, this site has some useful tutorials that could help you http://cplusplus.com/doc/tutorial/

EDIT 2 - Also, I forgot to mention that you need to put your code in code tags, it makes it much easier for people to read and you will get a lot more help if the code is pleasant to look at. To use code tags you just need to type
[code]
put your code here
[/code]
Welcome to the forum =)

EDIT 3 - Also, your for loop in countDown() is wrong, but I'll let the tutorials help you with that, no more edits now...honest





Last edited on
is this correct ?

//Debug6-1
// 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
#include<iostream>
using namespace std;
int main()
{
void countDown(int lowest,int highest);
int highest,lowest;
cout <<"I will count down from the higher number you enter to the lower one " << endl;
cout <<"Enter a number" << endl;
cin >> highest;
cout <<" Enter another number " << endl;
cin >> lowest;
if(highest<lowest)
{
std::swap(highest,lowest);

}
countDown(int highest,int lowest);
return 0;
}

{
int x;
for(x=highest; x=lowest;--x)
cout << x << "" << endl;
}
Firstly, when you call a function, you don't use types. So countDown (int highest, int lowest) ; should be countDown (highest, lowest) ;.

Those are backwards, by the way. countDown was declared as void countDown (int lowest, int highest) ;, but called as countDown (highest, lowest) ;. See the problem?

Thirdly, your loop needs a little work. In C++, the "=" is an assigning operator. This means that variable = 100 ; sets variable to 100, and x = lowest ; sets x to lowest. What you want is for (int x = highest ; x >= lowest ; x -- ). The ">=" checks to see if what's on the left is greater than or equal to what's on the right. This means that 5 < 4 will resolve to false.

Fourthly, you shouldn't declare a function in a function. countDown and any other methods you might add should be declared right after the include declarations.

Fifthly, the compiler won't know what you mean by that last "{" . You need to tell it that that's the implementation of countDown:
1
2
3
void countDown (int highest, int lowest) {
// implementation of countDown here
}


Your output and input commands could use a little cleaning:
1
2
3
4
5
cout <<"I will count down from the larger number you enter to the smaller one." << endl;
cout << "Enter a number: " ;
cin >> highest ;
cout << "Enter another number: " ;
cin >> lowest ;


Seventhly, since that if statement is only one line, you can pull the braces and shorten it to if (highest < lowest) swap (highest, lowest) ;

Eighthly, you already said you were using namespace std ;, so you can get rid of any "std :: "'s in your code.


I think that's all. Good luck!
Last edited on
i still have errors .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

//Debug6-1
// 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
#include<iostream>
using namespace std;
int main()
{
countDown(int highest,int lowest){
int highest,lowest;
}

cout <<"I will count down from the higher number you enter to the lower one. " ;
cout <<"Enter a number: " ; 
cin >> highest;
cout <<" Enter another number: " ;
cin >> lowest;
if(highest<lowest) swap(highest,lowest);


countDown(int highest,int lowest);
return 0;


{
int x;
for(int x=highest; x=lowest;x--)
cout << x << "" << endl;
}


Just FYI, it helps if you say what the errors are so we don't have to go copy-pasting into a new project.


This worked for me:
Reread the first, third, and fifth clauses of my previous post. Edit your code accordingly.

Stick ‘int’ onto the beginning of line 10, replace the "{" with a ";" and move the whole line to right after line 7.

Delete lines 11 and 12.

Declare highest and lowest in main () .

main () has no "}" at the end; add one.

Make countDown void, or return something at the end.
Topic archived. No new replies allowed.