C/C++ Thread

Cromewell

Administrator
Staff member
Please post any questions relating to C/C++ here. Please also specify which you are using and external libraries, if any, you are using. (Note: Visual C++ is slightly different than ANSI C++, generally your code will work between the two but certain functions you may want to use in VC++ wont be available in ANSI).

You can also use this thread to post code you wish to share.
 
Last edited:

fastdude

Active Member
EDIT: My question has been resolved by an in-the know friend.
Just to start the thread off:

#include <iostream>
using namespace std;
void main()
{
cout << "Hello computerforum!" << endl; cout << "Welcome to the C++ thread" << endl; }
 

Troncoso

VIP Member
I suppose this can also be the C# thread? As I just started a class in said language and I feel I'm really going to enjoy it and expand beyond the classroom.
 

Cromewell

Administrator
Staff member
I would probably put C# in with the .NET stuff. It's usually going to be managed code that fits better there. That said, if you think your stuff fits better here or there go with that one, it can always be moved if necessary.
 

Dystopia

Active Member
EDIT: My question has been resolved by an in-the know friend.
Just to start the thread off:

#include <iostream>
using namespace std;
void main()
{
cout << "Hello computerforum!" << endl; cout << "Welcome to the C++ thread" << endl; }

Looks good, but the way I am learning, you shouldn't be mixing the lines. Also I use Visual C++ so it is a tad different.

But, that should look like:
#include <iostream>
using namespace std;

void main() //actually I have a different string of stuff I have yet to learn what it means, but apparently Visual requires slightly different stuff

{
cout << "Hello computerforum!" << endl;
// I actually go and seperate them like this too, only the cin goes under cout, but thats prefrence. I think.
cout << "Welcome to the C++ thread" <<endl;

return 0; //You forgot this part, I think without it either, the program will just close, or, you won't have the message saying "press any button to continue......."
}

Monday I started learning how to set up a table of info, today I am supposed to be learning how to finish that.
 

Cromewell

Administrator
Staff member
Looks good, but the way I am learning, you shouldn't be mixing the lines. Also I use Visual C++ so it is a tad different.

But, that should look like:
Code:
#include <iostream>
using namespace std;

void main() //actually I have a different string of stuff I have yet to learn what it means, but apparently Visual requires slightly different stuff

{
cout << "Hello computerforum!" << endl;
// I actually go and seperate them like this too, only the cin goes under cout, but thats prefrence. I think.
cout << "Welcome to the C++ thread" <<endl;

return 0; //You forgot this part, I think without it either, the program will just close, or, you won't have the message saying "press any button to continue......."
}
Monday I started learning how to set up a table of info, today I am supposed to be learning how to finish that.
You're right, you should put everything on its own line for readability, the couts can be chained without really hurting how easy it is to read. However, the return call isn't needed in fastdudes post (or your updated code) because the main function is a void.



Your main probably looks more like:
Code:
int main(int argc, char *argv[])
Depending on what you are doing you may (or may not) want/need to return a value to the calling process to indicate whether your code succeeded or not. I'm pretty sure the standard says main always needs to return a value.
 
Last edited:

Dystopia

Active Member
You're right, you should put everything on its own line for readability, the couts can be chained without really hurting how easy it is to read. However, the return call isn't needed in fastdudes post (or your updated code) because the main function is a void.



Your main probably looks more like:
Code:
int main(int argc, char *argv[])
Depending on what you are doing you may (or may not) want/need to return a value to the calling process to indicate whether your code succeeded or not. I'm pretty sure the standard says main always needs to return a value.

Yup, that's my main all right.


Today I learned how to effectively use the <iomanip> function. Also worked more on the <cmath>.
 

Cromewell

Administrator
Staff member
Installed Visual Basic 2010. Currently learning how to use a switch instead of a if function.
It's a pretty useful block when you need to test 1 variable for a series of different values but if you have a complex check you need to use if statements. You can put multiple values in the same case if they all need to be treated the same. The break statements are also important because wihout them all the code below will execute, as seen in the second code block below.
Code:
switch (var) {
  case <value>: //if var = <value> code1 runs
    code1;
    break;
  case <value2>, <value3>: //if var = value2 or var = value3 code2 runs
    code2;
    break;
  default: //this is like an else on an if statement
    code3;
}

Code:
switch (var) {
  case <value>: //if var = <value> code1 runs
    code1;
    break;
  case <value2>, <value3>: //if var = <value2> or var = <value3> code2 and code3 will run
    code2;
  default:
    code3;
}
 

Dystopia

Active Member
It's a pretty useful block when you need to test 1 variable for a series of different values but if you have a complex check you need to use if statements. You can put multiple values in the same case if they all need to be treated the same. The break statements are also important because wihout them all the code below will execute, as seen in the second code block below.
Code:
switch (var) {
  case <value>: //if var = <value> code1 runs
    code1;
    break;
  case <value2>, <value3>: //if var = value2 or var = value3 code2 runs
    code2;
    break;
  default: //this is like an else on an if statement
    code3;
}

Code:
switch (var) {
  case <value>: //if var = <value> code1 runs
    code1;
    break;
  case <value2>, <value3>: //if var = <value2> or var = <value3> code2 and code3 will run
    code2;
  default:
    code3;
}

Yeah, that's what I learned too.

I'm up to the for/while/do-while loops. Really like that, no more having to restart the program to test every single input validation!

I've also learned how to make my own functions.
 

Troncoso

VIP Member
You learned things in a different order than I did. We wrote our own functions from the start. Programming is fun to me until I get to classes. Then I get sad
 

Dystopia

Active Member
You learned things in a different order than I did. We wrote our own functions from the start. Programming is fun to me until I get to classes. Then I get sad

Yeah, my instructor said from now on he wants us to use our own functions. Took me a bit to get how to do it, especially having several variables.

I enjoy programming too...and why do you get sad in class? I like it in class, where I can learn how to do new things. Although if I get a project that takes some time, I just do it on my laptop, so I can leave early.
 

brian

VIP Member
Yup, that's my main all right.


Today I learned how to effectively use the <iomanip> function. Also worked more on the <cmath>.

Hah, we just learned about that yesterday. So far I am enjoying the class, so far its stuff I already know but it helps to reinforce.
 

Cromewell

Administrator
Staff member
Yeah, my instructor said from now on he wants us to use our own functions. Took me a bit to get how to do it, especially having several variables.

I enjoy programming too...and why do you get sad in class? I like it in class, where I can learn how to do new things. Although if I get a project that takes some time, I just do it on my laptop, so I can leave early.

I think he meant he gets sad when he has to build classes in programming. You'll get to them later in your course work.
 

Troncoso

VIP Member
I think he meant he gets sad when he has to build classes in programming. You'll get to them later in your course work.

he's correct. I mean using classes in your program and creating objects and instances of those objects...when I was using gamemaker this stuff was way more fun. But in the applications that we made that used classes, they felt completely unnecessary, so I never really got a feel of their importance.
 

Dystopia

Active Member
Ok, I am supposed to write a program that will calculate the new total of a loan, by the end of each year. I am supposed to do the calculation each month, but display it each year. A function MUST be created for calculating the new balance (ie. new balance = old balance + old balance*rate - monthly payment). I can't make the stupid program calculate that in a function!! Here is the code I have written. Let me know if you guys need more comments:

Code:
// lab11.cpp : Defines the entry point for the console application.
//
//Adam Holthaus, steve05

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;


double balance(double, double, double, double);
int _tmain(int argc, _TCHAR* argv[])
{
	//Declaring variables
	double monthlyrate, monthlypay, m1, m2, m3, m4, rate, oldb, newb, loan, years, numpayments, apy, month, yrs;
	month = 1;
	newb = 0;	
	//Getting loan info
	cout <<"What is the value of the loan? $";
	cin >> loan;
	oldb = loan;
	cout <<"For how long will you have the loan? ";
	cin >> years;
	cout <<"What percentage is your APY? ";
	cin >> apy;
	//Getting monthly payment
	numpayments = years*12;
	monthlyrate = apy/1200;
	rate = monthlyrate+1;
	m1 = pow(rate, numpayments);
	m2 = monthlyrate*m1;
	m3 = m1-1;
	m4 = m2/m3;
	monthlypay = m4*loan;
	//Finding new balance
	for(yrs = 1; yrs <= years; yrs++)
	{
		while(month < 12)
		{
		newb = balance(newb, oldb, monthlypay, rate);
		month++;
		}

		cout <<newb <<endl;
	
	}

	return 0;
}


//newbalance.cpp calculates the new balance each month for one year, for lab11
//Adam Holthaus, steve05

#include "stdafx.h"
//Function
double balance(double newb, double oldb, double monthlypay, double rate)
{
	

	newb = oldb + oldb*rate - monthlypay;
	return newb;
}

I got the monthly payment right. That works. But the calculation screws up. I am using VisualStudio 2010. If you want to run it in your own compiler that is not VS, exchange my "main line" with yours, and remove the #include "stdafx.h" code.

Also, I have the code all together here, but in my compiler, I have the function separated the way I am supposed, I know that's not the issue.

This is just homework, not a test, and I just need help figuring out the function part.

Anybody figure out what I am doing wrong?
 

Cromewell

Administrator
Staff member
I'm guessing this is for simple interest (i.e. you have to pay off the interest accrued each year) instead of compound interest.

Check how you are handling the interest. Some of the calculations don't look right to me around your monthly payment comment around line 30.
 
Top