Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,110 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,722 people online right now. Registration is fast and FREE... Join Now!




Simple Input/Output

 
Reply to this topicStart new topic

Simple Input/Output, Trying to Create a Little Converter...

killned3
8 Jun, 2007 - 08:24 PM
Post #1

New D.I.C Head
*

Joined: 8 Jun, 2007
Posts: 45


My Contributions
Hey, everybody, glad to have joined the forums. :]

I've just recently started learning C++ and it seems like the one for me at the moment. However, I'm having a slight problem with this little program I've tried to create:

CODE

#include <iostream.h>
#include <math.h>

char songs;
double x = 5.5;
char megab = songs * x;

int main (void)
{
     cout << "Please input the number of songs in your iTunes library: ";
     cin >> songs;
     cout << "The amount of MB your iTunes contains is roughly ";
     cout << megab;
}


The first part works fine... the input doesn't seem to have any problems... but when it returns the converted amount to rough MB (and this is mostly just a wild guess and a chance to mess around with this kind of program), it returns a ¿. If anybody could let me know what I've done wrong, I would very much appreciate it. Thanks a lot. :]

Colt
User is offlineProfile CardPM
+Quote Post

Kayne
RE: Simple Input/Output
8 Jun, 2007 - 08:45 PM
Post #2

D.I.C Head
**

Joined: 15 Mar, 2007
Posts: 62


My Contributions
cout << "Please input the number of songs in your iTunes library: ";
cin >> songs;

Your having the user enter a number that is asigned to songs. But you made songs type char.

char songs;
double x = 5.5;
char megab = songs * x;

by having megab = songs * x, your again trying to asign a number to a char data type. Those two should be changed to either type int or double. I'm assuming you'll want to use int becuase you said roughly, and double would give you the exact amount.

Also, if you give megab the value of sounds * x before a value is put into sounds, then it will be the same has writing megab = 0 * x.
you should move megab = songs * x; don't after the user enters a number,

cout << "Please input the number of songs in your iTunes library: ";
cin >> songs;
megab = sounds * x;
cout << "The amount of MB your iTunes contains is roughly ";
cout << megab;


This post has been edited by Kayne: 8 Jun, 2007 - 08:45 PM
User is offlineProfile CardPM
+Quote Post

killned3
RE: Simple Input/Output
8 Jun, 2007 - 09:30 PM
Post #3

New D.I.C Head
*

Joined: 8 Jun, 2007
Posts: 45


My Contributions
Thanks a lot, Kayne... the thing that I wasn't sure of before was whether or not you could assign a name to an integer and now I remember that you absolutely can, especially since you name them with variables to begin with. The detail about the 0 * x and how it can't be used until a value has been put into songs was also something I wouldn't have been able to figure out on my own. I appreciate your help, and the program works now. :]

Colt
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Simple Input/Output
9 Jun, 2007 - 10:52 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
QUOTE(Kayne @ 9 Jun, 2007 - 12:45 AM) *


Also, if you give megab the value of sounds * x before a value is put into sounds, then it will be the same has writing megab = 0 * x.
you should move megab = songs * x; don't after the user enters a number,


There is no guarantee that a zero will be the value for songs...without being assigned a value, it could theoretically contain any value.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month