A quick question about C++

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1043 Views - Last Post: 04 August 2009 - 09:45 PM Rate Topic: -----

#1 Will38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-August 09

A quick question about C++

Post icon  Posted 03 August 2009 - 05:40 PM

I was reading my book on C++ (for Dummies) and I came across a part on calculating the average of three integers. The source-code seemed simple enough so i tried to edit it a little bit. Basically it looked like this:
int main(void)
{
int nValue1;
nValue1 = 1;

int nValue2;
nValue2 = 2;

int nValue3;
nValue3 = 2;

int nAverage;
nAverage = (nValue1 + nValue2 + nValue3) / 3;

cout << nAverage;
cin.get();

return(0);
}



keep in mind that this is just a snippet of the code, and it works properly how it is.

The thing is, i was trying to change the code so that the application would ask you for the values instead of imbedding them
within the code itself.

I tried something like this

int nValue1;
cout << "Please input the value of Value1"
cin >> nValue1;

// as soon as i input the value, the program just closes,
// even when that was the only thing i changed. 




And one other problem i ran into was when i tried to use the endl function, my IDE said
statement cannot resolve address of overloaded function.
int nAverage = (nValue1 + nValue2 + nValue3) / 3; endl;
cout << nAverage;
cin.get()



sorry for making such a long post, but if you could answer any of my questions i would appreciate it :D

Is This A Good Question/Topic? 0
  • +

Replies To: A quick question about C++

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: A quick question about C++

Posted 03 August 2009 - 05:56 PM

cout << "Please input the value of Value1"
missing ; here

int nAverage = (nValue1 + nValue2 + nValue3) / 3; endl;
why the endl; ? this isn't an output statement.
Was This Post Helpful? 1
  • +
  • -

#3 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: A quick question about C++

Posted 03 August 2009 - 06:00 PM

Is this what you wanted? endl must be within an output statement.
int nAverage = (nValue1 + nValue2 + nValue3) / 3;
cout << nAverage << endl;
cin.get();


This post has been edited by ericr2427: 03 August 2009 - 06:01 PM

Was This Post Helpful? 1
  • +
  • -

#4 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: A quick question about C++

Posted 03 August 2009 - 06:23 PM

Was the window closing because you didn't keep it open?

int main()
{
	using namespace std;
	 int c, d, e;
	 
	char name[30];
	cout << "Enter three values: ";
	int nValue1, nValue2, nValue3;
	cin >> nValue1 >> nValue2 >> nValue3;


	int nAverage;
	nAverage = (nValue1 + nValue2 + nValue3) / 3;

				cout << nAverage;
				cin.get();
				return 0;
}



Input: Enter three values: 3 3 3
Output: 3
Was This Post Helpful? 0
  • +
  • -

#5 Will38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-August 09

Re: A quick question about C++

Posted 03 August 2009 - 07:31 PM

hmm. I think i might have mis typed the code. If not thanks, that was a stupid mistake lol...

I thought the window shouldn't close because i used the cin.get() function. I thought cin.get() waited for user input before moving on? That's how it worked before i used endl; in the code.

In your modified version of the code, kidicarus, can you explain to me these parts of the code?

 using namespace std;	  // i thought you were supposed to write this before 
									  // int main()

 int c, d, e;	  // what are these for?

 char name[30]; // also what does this mean?



Sorry for asking, i just started C++ and I'm a noob at it =/
Was This Post Helpful? 0
  • +
  • -

#6 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: A quick question about C++

Posted 03 August 2009 - 08:04 PM

View PostWill38, on 3 Aug, 2009 - 06:31 PM, said:

hmm. I think i might have mis typed the code. If not thanks, that was a stupid mistake lol...

I thought the window shouldn't close because i used the cin.get() function. I thought cin.get() waited for user input before moving on? That's how it worked before i used endl; in the code.

In your modified version of the code, kidicarus, can you explain to me these parts of the code?

 using namespace std;	  // i thought you were supposed to write this before 
									  // int main()

 int c, d, e;	  // what are these for?

 char name[30]; // also what does this mean?



Sorry for asking, i just started C++ and I'm a noob at it =/


using namespace std; outside of the main function justgives it a global scope so when you create other functions you don't have to keep writing using namespace std;

A char is a single letter,number,symbol etc... so here you can enter numbers 30 digits long

I don't know why he put int c,d,e; it works without it
Was This Post Helpful? 1
  • +
  • -

#7 Will38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-August 09

Re: A quick question about C++

Posted 03 August 2009 - 08:26 PM

Why use char name[30]; ?
it works fine without it? also, the window still closes right after it shows the average, without giving me time to see it, which only solves half my problem.
Was This Post Helpful? 0
  • +
  • -

#8 pakkuman_shinde  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 06-May 09

Re: A quick question about C++

Posted 03 August 2009 - 08:57 PM

Shouldn't it be more like:

#include <iostream>
#include <stdio.h>
int main(){
	using namespace std;
	cout << "Enter three values: ";
	int nValue1, nValue2, nValue3;
	cin >> nValue1 >> nValue2 >> nValue3;
	nValue1 = (nValue1 + nValue2 + nValue3) / 3;

				cout << nValue1;
				cin.get();
				return 0;
}


Was This Post Helpful? 1
  • +
  • -

#9 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: A quick question about C++

Posted 03 August 2009 - 09:05 PM

I help other people on this forum and sometimes I test my suggestions before I submit them, and in this case I acidentally had some code left over from helping somebody else.

Sorry about that I didn't mean to cause any confusion.

Anyway, as mentioned you can use "using namespace std;" within the main function. Some books actually recommend this practice for various reasons.
Was This Post Helpful? 1
  • +
  • -

#10 Will38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-August 09

Re: A quick question about C++

Posted 04 August 2009 - 12:28 PM

thanks guys for all your help! Not a problem kidicarus, you still helped me a lot :)
Was This Post Helpful? 0
  • +
  • -

#11 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: A quick question about C++

Posted 04 August 2009 - 12:38 PM

View Postpakkuman_shinde, on 3 Aug, 2009 - 07:57 PM, said:

Shouldn't it be more like:

#include <iostream>
#include <stdio.h>
int main(){
	using namespace std;
	cout << "Enter three values: ";
	int nValue1, nValue2, nValue3;
	cin >> nValue1 >> nValue2 >> nValue3;
	nValue1 = (nValue1 + nValue2 + nValue3) / 3;

				cout << nValue1;
				cin.get();
				return 0;
}



No, it should look like this, i'm pretty sure. Atleast this is how I have been taught.
#include <iostream>
#include <stdio.h>

using namespace std;
int main() {
/* code goes here */
return 0;
}



The using namespace std; Should be outside the main function.

This post has been edited by IngeniousHax: 04 August 2009 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: A quick question about C++

Posted 04 August 2009 - 01:10 PM

after you figure out a solution, its good practice to look into new methods.

#include <iostream>
#include <vector>

using namespace std;
class average
{
 public:
	 int input(int size)
	 {
		vector<int>index(size);
		int total = 0;
		for(int i=0; i<size; i++)
		{
			  cin>>index[i];
			  total +=index[i];
		}
		return total/size;
	 }
}; average avg;

int main()
{
	int amount; 
	cout <<"Amount of Numbers :";
	cin>>amount;
	cout<<"Average :"<<avg.input(amount);

	cin.sync();
	cin.get();
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#13 Will38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-August 09

Re: A quick question about C++

Posted 04 August 2009 - 01:22 PM

haha ok... that's pretty confusing to me (i only started this a few days ago) but i agree it's good to always look for new ways to solve the same problem.

and @ ingenioushax,

Quote

Anyway, as mentioned you can use "using namespace std;" within the main function. Some books actually recommend this practice for various reasons.

Was This Post Helpful? 0
  • +
  • -

#14 dwayne  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 02-August 08

Re: A quick question about C++

Posted 04 August 2009 - 01:59 PM

You can totally omit the "using namespace x;" and just add and "std::" prefix to the commands (e.g. "std::cin.get();".
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: A quick question about C++

Posted 04 August 2009 - 02:04 PM

oh my bad lol, i didnt read your hole post. But the next step is try to create a function. Its very simple.
#include <iostream>

// global namespace
using namespace std;

// You must always declare the function before you use it,
// Just like any other variable.
int average(int Num1, int Num2, int Num3) // you Parameters 
{
	// the Nums are replace with the values you passed in

	int total = (Num1 + Num2 + Num3)/3; // the math

	//you return value is always the same type as your funciton type
	// in this case its an int, so you return back an int

	return total; // this is what you return back to the main()
}
int main()
{
	cout << "Enter three values: ";
	int nValue1, nValue2, nValue3;
	cin >> nValue1 
		 >> nValue2 
		 >> nValue3;
	cout<<"Total :"<<average(nValue1,nValue2,nValue3);
	
				//Just to pause the window
				cin.sync();
				cin.get();
				return 0;
}


This post has been edited by ImaSexy: 04 August 2009 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2