6 Replies - 1818 Views - Last Post: 04 September 2012 - 07:51 PM Rate Topic: -----

#1 violatedsmurf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-August 12

Runtime error help

Posted 29 August 2012 - 05:47 PM

I keep getting this error "Run-Time Check Failure #3 - The variable 'num1' is being used without being initialized." and have no clue what I am doing wrong.

#include <iostream> 
#include <iomanip> 

using namespace std;

int main()
{

	int num1, num2, num3, num4, num5;
	int sum = num1 + num2 + num3 + num4 + num5;
	int avg = sum/5;

	cout << "Enter five decimal number: "<< endl;
	cin >> num1 >> num2 >> num3 >> num4 >> num5 ;
	cout << "num1" << "num2" << "num3" << "num4" << "num5" << endl;

	setprecision(2);
		cout << "The sum is:" << num1 + num2 + num3 + num4 + num5 << endl;
		cout << "The average is: " << avg << endl; 

		return 0; 

}



Is This A Good Question/Topic? 0
  • +

Replies To: Runtime error help

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Runtime error help

Posted 29 August 2012 - 05:56 PM

int num1, num2, num3, num4, num5; //uninitialised



int num1=0, num2=0, num3=0, num4=0, num5=0; //initialised


Was This Post Helpful? 0
  • +
  • -

#3 violatedsmurf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-August 12

Re: Runtime error help

Posted 29 August 2012 - 06:01 PM

I seriously need to learn to pay more attention, any trick some could share? It does not get any easier does it?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,236
  • Joined: 23-August 08

Re: Runtime error help

Posted 29 August 2012 - 06:05 PM

No, it does not get any easier.

Simply put, you can't do calculations on variables that don't have values. It makes no sense whatsoever to calculate sum or avg before you've received the user's input.
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Runtime error help

Posted 30 August 2012 - 01:38 AM

View Postsnoopy11, on 30 August 2012 - 02:56 AM, said:

int num1=0, num2=0, num3=0, num4=0, num5=0; //initialised



If you don't intend a variable's value to be 0, don't initialize it to 0. All this will do is hide the real error.

As Jack said, all calculations on the nums should happen after they're read from cin. Setting them to 0 will just make the error message go away and set sum and avg to 0, instead of the value they're supposed to have.
Was This Post Helpful? 0
  • +
  • -

#6 violatedsmurf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-August 12

Re: Runtime error help

Posted 04 September 2012 - 04:53 PM

Do I need to
int
each number for the users input?
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Runtime error help

Posted 04 September 2012 - 07:51 PM

You program starts its execution from the top of main, and works line by line executing the set of instructions until you hit return. You can't calculate anything until you get input from the user first
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1