Quick Question, Assignment due soon

Pages: 12
Mar 6, 2010 at 7:57pm
Hey guys,
When I compile this code, it says it compiles successfully yet when I run with debugging, it says
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\Chris\Documents\Cmpcsci101\Assignment12\Debug\Assignment12.exe : fatal error LNK1120: 1 unresolved externals

I cannot figure out what is wrong. My program is suposed to let the user input however many numbers, and then display the minimum and maximum number. Can somebody please help me ? My code is:

# include <iostream>
using namespace std;

double num, maxv, minv, count, sum;
char answer;
int N;

int main()
{
for (count=1; count<N; count++)
{
sum = 0;
cout<<"Please enter the number." <<endl;
cin>>num;

if (num < minv)
minv = num;
if (num > maxv)
maxv = num;

cout<<"Do you want to enter another number (y/n)? "<<endl;
cin>>answer;

}
while (answer == 'y' || answer == 'Y');

cout<<"You entered" <<N<< " numbers. "<<endl;
cout<<"The maximum number was " <<maxv<< "and the minimum number was " <<minv<< ".";

return 0;
}


Thanks so much for anybody that helps!

Mar 6, 2010 at 8:27pm
Did you create the project as a Win32 application, or as a Win32 Console application?
Mar 6, 2010 at 8:30pm
its a win32 application/project
Mar 6, 2010 at 8:33pm
Create it as a Win32 console application. Otherwise, the compiler expects an entry point other than main(). Specifically, WinMain().
Mar 6, 2010 at 8:36pm
it atleast brought up the command prompt but only displayed "you entered 0 numbers. The maximum number was 0 and the minimum number was 0"

any advice on my code? It might not be right im really a novice at this. Thanks so much!!!
Mar 6, 2010 at 8:50pm
You never initialize N, so it could be anything.
Mar 6, 2010 at 8:54pm
well its suppossed to let the user input as many numbers until they enter anything other then 'Y' or 'y'

did i not do that correctly? How would I go about doing that?
Mar 6, 2010 at 9:02pm
If N does not have a defined value, any behavior accessing it is undefined. The program doesn't know what you want N to be or when it "should" be a certain value. You'd be better off with a while. For loops are designed to run until a determinate number of iterations. Check the tutorial on this site if you don't know what a while is.
Mar 6, 2010 at 9:02pm
You are using a for loop, not a while loop there. You while at the end doesn't do anything at all.
Mar 6, 2010 at 9:06pm
so what should I change to make it work? A do-while loop? How would I alter the count=1, count<N, count++ to make it work for a do? Im too new to this to understand but I am getting really frustrated and Just want it to work
Mar 6, 2010 at 9:46pm
A perfectly normal while loop would do. Your condition should be to check if the user inputted y or Y. Instead of incrementing a counter of inputted items in the loop statement, put it somewhere in the loop code section.
Mar 7, 2010 at 12:47pm
hi there highbies!!!! well any loop will acutally do it but i would suggest to use for-loop... for the algorith, try to swap the index of arrays.. and do it by making a while loop within a for loop... in the while loop you will try to make some conditions when to swap the index of arrays. jah get it???

for example:

9 8 7 6 3

if 9 > 8 then swap 9 to 8's place.
if 8 > 7 then swap 8 to 7's place. and so on... hope jah get it...

and to the highbies... have i done the right thing sir?
Mar 7, 2010 at 5:04pm
Uh, why would he use a for loop when the number of inputs is indeterminate? There's no point in keeping an array of the numbers. Only two numbers are necessary - the highest and the lowest. His current method of keeping track of them is more efficient and runs a tighter memory footprint than maintaining a list of all the numbers that have yet been inputted. In addition, since an array is not expandable an array would be a useless solution because, again, the input number is unknown. (Technically the solution to that would be to use a vector but that is unnecessary.) Although technically it could be done with either, while loops are better and certainly better style for an unknown number of iterations, as opposed to for loops whose syntax is designed to run a certain, determined number of times.
Mar 8, 2010 at 3:41am
OIC sir... i havent used vector... as i see from his source code we are on the same level of learning c++ we havent taught yet of such vectors... as for us we determine it by using vectors
Mar 8, 2010 at 4:09am
If you replace the for line with "do", your looping will work. Next, sum is initialized to zero inside of the loop; it should be initialized before the loop and num should be added to it each iteration. Lastly, initialize another variable to zero before the loop and increment it inside the loop each iteration to get the total count. Note that you'll also want to initialize maxv and minv.
Last edited on Mar 8, 2010 at 4:11am
Mar 8, 2010 at 4:46am
thats it... well should i post the source code sir hohum???? ahehehehe!!!
Mar 8, 2010 at 7:11pm
Hi liltmac100,

I'm new to C++ and wanted some more practice writing code. Here is my two-cents with regards to writing your program. I'm not sure if it's the absolute best way to write it, but it works.

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
31
32
33
34
35
36
#include <iostream>
using namespace std;

int main(){

	double max, min, val, avg = 0;
	int counter = 0;
	char cont;

	cout << "This program lets you enter values until your fingers bleed!";

	do {
		cout << "\n\nPlease enter a number: ";
		cin >> val;
		counter++;
		cout << "\n";

		if(counter == 1){ //initialise values of min and max on first run through, but not on subsequent runs
			min = val;
			max = val;
		}

		if(val < min) min = val;
		if(val > max) max = val;

		avg += val;

		cout << "Do you wish to continue(y/n)?";
		cin >> cont;

	} while(cont == 'y' || cont == 'Y');

	cout << "\nYou entered " << counter << " numbers.\n\n";
	cout << "The highest was " << max << " and the lowest was " << min << "\n\n";
	cout << "The average of the numbers is " << avg / counter << "\n\n";
}
Last edited on Mar 8, 2010 at 7:12pm
Mar 8, 2010 at 7:42pm
@Timminski: Don't just post code, it doesn't help the OP learn anything.
Mar 8, 2010 at 8:14pm
my bad. Do you think you can help me with a problem i posted the other day? No replies thus far - it's still on the first page of 'beginners' and has the title 'if criteria problem in recursive function'. Thanks.
Mar 8, 2010 at 9:37pm
Don't hijack threads.
Pages: 12