Error "C4700: uninitialized local variable"

Hello people, this is my first time using this site so cut me some slack if I have trouble. I am in currently Foundations and Computer Programming II learning how to use C++ in more advance ways. I been given my first assignment and I've already came across an error that I don't know how to fix. The error is "C4700: uninitialized local variable 'n2' used" and here is what I have.


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "multiply.h"
using namespace std;
int main()
{
	//lab1 part1, I am writing a program to multiply 2 numbers.
	int n1, n2;
	cout << "Enter in 2 numbers and I will multiply them" << endl;
	cin >> n1, n2;
	multiply(n1, n2);
	system("pause");
	return 0;
}

multiply.h

void multiply(int, int);

multiply.cpp

1
2
3
4
5
6
7
8
#include <iostream>
#include "multiply.h"
using namespace std;
void multiply(int& n1, int n2)
{
	n1 = n1*n2;
	cout << "The product of those 2 numbers is " << n1 << endl;
}

Can anyone please help me figure out what I am doing wrong.
Last edited on
Hey and welcome to this forum. Please make sure to read this post - http://www.cplusplus.com/forum/beginner/1/

And to always use code tags for all of your code - http://www.cplusplus.com/articles/jEywvCM9/

(You can edit your post now and use them to make it more readable)

cin >> n1, n2;

This is not how the coma operator works. What you want is

cin >> n1 >> n2;
Thank you for that tip Tarik, but now I have come across 2 more errors when I made that change. Both of them are error "LNK2019: unresolved external symbol."

One error says 'WinMain@16 referenced in function "int_cdel invoke_main(void)" (? invoke_main@@YAHXZ)'

The other one says '"void_decl multiply(int,int)" (?multiply@@YAXHH@Z) referenced in function_main'

I think this might be more of a file error, but I'm not sure. I should probably start another forum.
I should probably start another forum.


What does that even mean?

Your problem is simple, your function prototype and your function definition do not look idenetical, which they have to.

void multiply(int, int); // takes 2 integers

void multiply(int& n1, int n2) // takes 2 integers, but the first one by reference

They have to look the same.


void multiply(int& n1, int n2); // change your function that is in multiply.h to this
Well there goes one of the errors. But the first one is still there.

'WinMain@16 referenced in function "int_cdel invoke_main(void)" (? invoke_main@@YAHXZ)'

Last edited on
This means that you've selected the wrong kind of project. You're using Visual Studio version something; if you want to use a main function, you needed to create a simple, plain console project.
Topic archived. No new replies allowed.