5 Replies - 226 Views - Last Post: 10 July 2012 - 12:13 AM Rate Topic: -----

#1 persianmess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 12

Need help with this program

Posted 09 July 2012 - 03:12 AM

Hey everyone, so I've done the most I could do, but I still don't know how to add string, bool, and char into this equation. Could you please help.

Here are the instructions:
1. declare a char, an int, 3 float, a double, a string and a bool.
2. assign values to each of these. This should be done in separate steps from the declaration.
3. print each variable to the monitor preceeded by a statement saying what you are printing. Example: “here is my char: “ << char;
4. 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.
5. divide one float by another and put the result in the 3rd float. hint – you can reuse variables if you don’t need their value anymore. Print the result to the screen with a statement like: “x/y = “ x followed by an endline.
6. perform any other mathematic operations you feel like and print result to screen (but tell me what I’m seeing).


And here's what I've come up with so far:



#include <iostream>
 using namespace std; 
int main()
 
{
 
 int i;
 float x, y, z;
 double d;
 
 i = 15;
 d = 40;
 x = 25;
 y = 125;
 z = y/x;
 
 cout << "Here's my int: " << 'i' << endl;
 cout << "Here's my first float: " << 'x' << endl;
 cout << "Here's my second float: " << 'y' << endl;
 cout << "Here's my third float: " << 'z' << endl;
 cout << "Here's my double: " << 'd' << endl;
 cout << endl;
 
 cout << "The size of my int is: " << i << endl;
 cout << "The size of double is: " << d << endl;
 cout << "The size of my first float, x, is: " << x << endl;
 cout << "The size of my second float, y, is: " << y << endl;
 cout << "The size of z, which is y/x, is equal to: " << z << endl;
 
return 0;
 
}


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 09 July 2012 - 03:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with this program

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Need help with this program

Posted 09 July 2012 - 05:20 AM

17 cout << "Here's my int: " << 'i' << endl;
Remove the single quotes, like
cout << "Here's my int: " << i << endl;

24 cout << "The size of my int is: " << i << endl;
Investigate the sizeof operator, say by writing sizeof(i)


Did you even bother to run this code, to find out what it did?
Was This Post Helpful? 0
  • +
  • -

#3 persianmess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 12

Re: Need help with this program

Posted 09 July 2012 - 01:40 PM

Oh! So that's how you calculate the size of something! Makes sense, but what exactly does sizeof(1) portray? For instance, what are we doing that for?

I'm removing the '_' section completely, because I think it was wrong anyway.


My question now, is how do I add a string, bool, and a char to this example?






"17 cout << "Here's my int: " << 'i' << endl;
Remove the single quotes, like
cout << "Here's my int: " << i << endl;

24 cout << "The size of my int is: " << i << endl;
Investigate the sizeof operator, say by writing sizeof(i)"
Was This Post Helpful? 0
  • +
  • -

#4 persianmess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 12

Re: Need help with this program

Posted 09 July 2012 - 02:54 PM

View Postpersianmess, on 09 July 2012 - 01:40 PM, said:

Oh! So that's how you calculate the size of something! Makes sense, but what exactly does sizeof(1) portray? For instance, what are we doing that for?

I'm removing the '_' section completely, because I think it was wrong anyway.


My question now, is how do I add a string, bool, and a char to this example?






"17 cout << "Here's my int: " << 'i' << endl;
Remove the single quotes, like
cout << "Here's my int: " << i << endl;

24 cout << "The size of my int is: " << i << endl;
Investigate the sizeof operator, say by writing sizeof(i)"





Awesome! It worked! Thank you so much!!!

Last question .. I declared my int i;
i = 15;

When I use the statemnt:
cout << "The size of my int is: " << sizeof(i) << " characters." << endl;

It says that the size of my characters is 4 .. shouldn't it be 2, because I only have 2 numbers?
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Need help with this program

Posted 09 July 2012 - 11:13 PM

sizeof() tells you the amount of storage space in memory a type/object occupies in memory.

If you want a result where 12 is 2, 123456 is 6 (ie, the number of digits in the number), then you need another approach.
Was This Post Helpful? 0
  • +
  • -

#6 Huskerfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 31-January 12

Re: Need help with this program

Posted 10 July 2012 - 12:13 AM

Did you also figure out your other question about how to add other variable types? It's the exact same as you have been declaring the other variables.


#include <string>;

bool bSomething;
char chSomethingElse;
string sFinalThing;

bSomething = true;
// ... Etc. 



Just make sure you know that to declare a string, you must include the string header file
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1