hi i have a question about C++. if anybody could pplease help me out id highly appreciate it. this is what i am trying to do and honestly I have no idea how to do it or how to go about it:
Write a C++ program that does the following. Besides #include <iostream>, you will need #include <string>. Call this program program1.cpp

1. declare a char
2. declare an int
3. declare a float
4. declare a double
5. declare a string
6. declare a bool
7. assign values to each of these (separate statements from the declarations above)
8. print each variable to the monitor followed by an endline
9. assign a char to your int variable
10. print out that variable with statement like: “here is my char stored in an int variable”.
11. for each variable, write a statement that will print it’s size to the monitor. The statement should say, “the size of x is: “ followed by variable, followed by an endline.
12. add the two ints together and divide the total by 2. Print that result to the screen with a statement like: “(x+y)/2 = “ followed by result and an endline.

To declare variable types you just put

int myInt;

Where int is the type and myInt is the name of the variable.

To assign a value to a variable you can do it two ways

int myInt = 5; // if has not been declared already
myInt = 5; // must have been declared before

to print to the screen you put

cout << myInt << endl; // this will print 5
cout << "the value of myInt is " << myInt << endl;

If you don't type using namespace std; you have to type std::cout and std::endl because they belong to the std namespace.

To print a int value as a char you have to do type casting on the int variable.

cout << (char)myInt

This will print out a clubs symbol but if you want to see some letters start at 65 and work your way up.

For printing out the size of variables you use the sizeof(x) function.

sizeof(int);
sizeof(myInt);
cout << sizeof(myInt) << endl;
myInt = sizeof(myInt);

Just showing a few ways you can use this and other functions.

To print out (x+y)/2 you could put

cout << "(x+y)/2 = " << (myInt+myInt2)/2 << endl;

Where myInt and myInt2 are your 2 integers.

If you want to go over all this and lots more you can go to http://www.cplusplus.com/doc/tutorial/ its where I learned C++ and it has lots of useful information.

Hope this helped!

Hi sorry to bother you but I had a question..... so I rewrote the whole thing bafter looking through the website you posted and some help from someone else but Im still having trouble with some points that I needed to do. I was wondering if you could help me. First Ill post what I need help with and the n I will post the code that I have done.

9. assign a char to your int variable
10. print out that variable with statement like: “here is my char stored in an int variable”.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	char Character;
	Character = 6;
	
	

	int Integer;
	Integer = 7;
	
	float Float;
	Float = 3.1459;
	
	double Double = 307;
	Double = 307; 
	
	string String;
	String = "this is my string";

	bool Boolean= true;
	Boolean = true;

		
				int Integer2 = 69;

	cout << "Character: " << Character << endl;
	cout << "Integer: " << Integer << endl;
	cout << "String: " << String << endl;
	cout << "Float: " << Float << endl;
	cout << "Boolean: " << Boolean << endl;
	cout << "Double: " << Double << endl;
	
	cout << "sizeof Character:" << sizeof(Character) << endl;
	cout << "sizeof Integer:" << sizeof(Integer) << endl;
	cout << "sizeof Float:" << sizeof(Float) << endl;
	cout << "sizeof Double:" << sizeof(Double) << endl;
	cout << "sizeof String:" << sizeof(String) << endl;
	cout << "sizeof Boolean:" << sizeof(Boolean) << endl;
	
	cout << "(x + y)/2 = " << (Integer + Integer2)/2 << endl;

	return 0;
}

IT seems to work I dont know if what I did is correct but it does compile and everything.. the only thing is I still cant figure out how to print the char to and int and write what I have to write.. I hope you can help

The question is to assign a char, to an int variable.
Meaning you will need to declare an int:

int myInt;

And then assign a char to it:

myInt = 'c';
// or:
char myChar = 'c';
myInt = myChar;

The point of this assignment is to show you that C++ can do implicit conversions between some datatypes. Sometimes this can be handy but in a big program it can become unclear when it is performing an implicit conversion.. and it is not always clear to the reader of your code if this was your intention.
That's why in C++ explicit conversions are preferred normally. If you're interested you can look up 'static_cast', 'dynamic_cast' etc.

Edit:

Printing out a char is just as easy:

char myChar = 'c';
int myInt = myChar;

cout << "myChar: "<<myChar<<" myInt: "<<myInt<<"\n";

IT seems to work I dont know if what I did is correct but it does compile and everything.. the only thing is I still cant figure out how to print the char to and int and write what I have to write.. I hope you can help

I don't get this question, rephrase, yeah I was the one who whipped up that sample, what is it that your having a problem with? Be specific, I am a very specific man, unfortunately my wife doesnt like it but it gets things done lul =P

However I do think you need to contrast the difference between char's and int, and what you are trying to accomplish here. When you asked this question what got me confused "the only thing is I still cant figure out how to print the char to and int and write what I have to write" what are you trying to accomplish.

Pay close attention to this quote: Beware of the person who can't be bothered by details.
William Feather

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.