How to show the location of error? What's wrong with the program?

I am a C++ beginner. And I am a Hong Kong citizen, so my English is not so good. Sorry.

1. How can I show the location of error(s) in an error-existing program?

2. And, what's wrong with the program below:

#include <iostream>
using namespace std;

int main(){
bool ans=false;
int a;
int sum=0;
for (int i=1 ; i<101 ; i++){
sum = sum+i;
}

do{
cout << "1+2+3+...+99+100 = ";
cin >> a;
if a != sum
cout << "You are wrong. Try again. \n";
else {
cout << "You are right!! \n";
ans=true;
}
}while(!ans);

return 0;
}


2 errors existing.

Thx.
i think you forgot to enclose the if condition in parentheses
if (a != sum)
you are right!
i will remember to enclose the if condition with brackets forever.
THX!

Also,
how can I show the error location in an error-existing program?
Most compilers should give you the line and error... What IDE are you using? (like Code::Blocks, Dev C++, ect.)

Also, use the [ code ] [ /code ] tags (without the spaces)
Last edited on
I am using Microsoft Visual C++ 6.0.
And i don't know what is code tags.
Code tags are used to format c++ code so that it's lines are numbered and the syntax is highlighted. It's how you get stuff like this:

1
2
3
4
5
6
7
8
#include <iostream>

int main(){
	char a;
	cout<<"Hello World!"<<endl;
	cin>>a;
	return 0;}

You just type "[ code]," paste your code, and type "[ /code]," or use the <> button to the right.


As for displaying errors, it should show them at the bottom of your screen. You probably have it hidden, so try finding something along the lines of "error" or "log". For Code::Blocks, it's View|Logs or F2.
Last edited on
sorry that i don't understand to type the code to where.
I get your point now.
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
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main (){
	bool meaningless = false;
	int ans,guess,times;
	times=0;
	srand ( time(NULL) );
	ans = rand()%100 + 1;
    cout << "guess a number which is ranging from 1 to 100 \n";
  
	do{
		times = times+1;
		cin >> guess;
		if (guess > ans)
			cout <<"smaller please \n";
		else if (guess < ans)
			cout <<"bigger please \n";
		else {
			cout << "you are right after " << times << " times of guessing \n";
			meaningless=true;
		}
	}while (!meaningless);

	return 0;
}


Like this.

I can also see the errors now. Thank you very much!
Last edited on
Topic archived. No new replies allowed.