SRL Archive - C/C++ Help and Tutorials

Very basic addition


Overtime

2011-08-30 19:28:22

hello guys so you guys saw my Celsius to Fahrenheit converter to get a simple head start on C++ i thought i was taking it spring of 2012 but i ended up finding out that im taking it right now :O

Im so excited, and first day of class was more of a review to me as other were paused on what they got themselves into. Lol

so here is the source, dont feel like making a .exe so just run it on your C++

////////////////////////////////////////////////////
// *James Luna* //
// August 30, 2011 //
// CSCI 1370 //
// //
////////////////////////////////////////////////////

#include <iostream>

using namespace std;

int main()
{
int x, y, z;

// This is what x is going to represent for when we add them.
x= 67;

// This is so "x" is displayed on the console screen followed with a break to next line.
cout <<" x = " << x << endl;

// This is what z is going to represent for when we add it to x and 213.
z= 45;

// This is so we can see z on the console screen followed with a break to next line.
cout <<" z = " << z << endl;

// YES finally this is the statement we are using to add x+z+213 together so it can =y :)
y= x + z + 213;

// This is so we can see the end result on what y= :)
cout<<" y = " << y << endl;

// Simple command to pause the console.
system("PAUSE");

return 0;

}

i luffs yeww

2011-08-30 19:33:16

:< still using system("PAUSE");.

Other than that, yay. :p

Overtime

2011-08-30 19:36:33

This is a simple exercise that my professor asked us to do. She wants us to use what we just learned. So she hasn't gotten that advanced yet :P

Oh and this assignment is due Thursday and she is going to ask us to do this tomorrow, im just getting a head start since she posted tomorrow's slides up on what she is going to lecture :D

EDIT:

PFFFT OMZJKLG!

She is repeating her self again

Write C++ statements to do the following:

&#8211; Declare an integer variable called a

&#8211; Declare an integer variable called b

&#8211; Assign a the value 56

&#8211; Assign b the value 22 added to the value of a

&#8211; Print out the value of b


Edit 2: I believe this is what she wanted




#include <iostream>

using namespace std;

int main()
{
int a, b;

a= 56;

cout <<" a = " << a << endl;

b= 22 + a;
cout <<" b = " << b << endl;

system("PAUSE");
return 0;
}

NCDS

2011-08-30 20:07:39

From: Overtime

Edit 2: I believe this is what she wanted



#include <iostream>

using namespace std;

int main()
{
int a, b;

a= 56;

cout <<" a = " << a << endl;

b= 22 + a;
cout <<" b = " << b << endl;

system("PAUSE");
return 0;
}

She never asks you to print the value of 'a', yet you do anyways. Not sure if that would effect your grade or not; just pointing it out. ;)

Overtime

2011-09-01 01:10:11

hmm that is true, eh i dont know if it will affect my grade unless she's anal about it. But she seemed to be impressed with the work i turned in, since i was the only one that jumped ahead xD

I tried explaining my partner in class what i was doing, and he is lost.

I really dont see how he can be lost, the commands are all there! and there basic ones too!

Echo_

2011-09-01 11:55:55

You can do this as well:

#include <iostream>

using namespace std;

int main()
{
int a = 56, b = a + 22;

cout <<"b = " << b << endl;

system("pause");
return 0;
}

At least that's how I would have done it.

Overtime

2011-09-01 14:09:30

yea thats another way :)

She just wants us to know how to represent integers and stuff. Also wants to see a= and b= on the screen, so you could also add

cout << "a = " << a << endl;

superuser

2011-09-03 15:44:17

From: Echo_
You can do this as well:

#include <iostream>

using namespace std;

int main()
{
int a = 56, b = a + 22;

cout <<"b = " << b << endl;

system("pause");
return 0;
}

At least that's how I would have done it.


Few things, drop the return. If you don't have use for it, standard says it's not necessary.

Many people, instructors included, for some inexplicable reason think that making a call to the operating system and running system command to temporarily halt program is someway a good thing. Where they get this idea is beyond me.

Instead, use the functions that are defined natively in C++ already.
Like
std::cin.get()
.

You know, when you call system() the following will happen:

Suspend your program -> Call the OS -> Open OS shell (new sub-process) -> OS must now find the PAUSE command -> allocate memory to execute the command -> execute the command and wait for a keystroke -> free the memory -> exit the OS and finally resume your program.

Using namespace std is also evil. The whole point of namespaces is to prevent namespace collisions between two independently developed piles of code. Therefore, it's good practice to not use it.

#include <iostream>

int main()
{
int a = 56, b = a + 22;

std::cout << "b = " << b << std::endl;
std::cin.get();
}

bevardis

2011-09-07 09:48:00

Few years ago, I never thought programming could be addictive. But solving problems is fun. Sometimes in math I look forward to finding the X.