PDA

View Full Version : C++ Help



Cruise Control
02-07-2006, 04:42 PM
I need help in C++. I'm trying to figure out how to detect if a number entered by the user is an integer.

BTW we can't have equations in IF statements.

I don't know how to go about this or what to do from here.


//Chris Taylor
//2/6/06 (0010/0001/0110)
//Terminal 2
//Integer Recognition Program

//Standard header files
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>


int main()
{
ofstream outfile ("IntegerProgram.txt"); //Creates textfile
cout << "This is the semi-godlike Integer Detection Program. It will allow you to enter a number, then determine if the entered number is an intege.\n";


float integer;
int remainder;

cout << "Please enter your number: ";
cin >> integer;

remainder = integer/2;

cout << remainder;

getch();
return 0;
}

Flying Mullet
02-07-2006, 04:51 PM
At first glance, I believe that doing a mod by 1 will work. That will return any remainders you may have, then you can compare the remainder against 0 to see if there is one.



cout << "Please enter your number: ";
cin >> integer;

remainder = integer%1;
if(remainder == 0)
{
// do integer stuff
}
else
{
// do non-integer stuff
}

Cruise Control
02-07-2006, 04:58 PM
Actually, my teacher said I need to typecast a non integer in order to use modulus, what the hell is that? And how do I do this.

Flying Mullet
02-07-2006, 05:07 PM
Typcasting is "forcing" data of one type into another. For example, below I am forcing a float of 1.5 to an int of 1.

float number1 = 1.5;
int number2 = (int)number1;
And if you did the opposite, typecast the number 1 (an int) to a float, you would get 1.0.

I'm pretty sure you could do operator overloading to typecast entire objects, but it's been years since I've worked with C++.

And apparently you can't mod a float. I've researched modulus a little more on the web. Here's a thread in another fourm I found on the issue:
http://www.gamedev.net/community/forums/topic.asp?topic_id=306374

Like I said, it's been a while since I've used C++ so I'm still a bit rusty. Hope this helps.

Cruise Control
02-07-2006, 05:13 PM
It's not working, god I suck. I should reread the entire book.


int main()
{
ofstream outfile ("IntegerProgram.txt"); //Creates textfile
cout << "This is the semi-godlike Integer Detection Program. It will allow you to enter a number, then determine if the entered number is an intege.\n";


float integer;
int remainder = (int)integer;

cout << "Please enter your number: ";
cin >> integer;

remainder = integer%1;

if(remainder == 0)
{
cout << "Yes, " << integer << "is an integer.";
}
else
{
cout << "I'm sorry, " << integer << "is not an integer.";
}


getch();
return 0;
}

Flying Mullet
02-07-2006, 05:19 PM
Well you could always use a brute force approach.

1) Treat the input as a string.
2) Go through the input character by character and verify that each character is only 0-9 or a ".".
3) If there is a ".", check that there is only one ".".
4) If there is no decimal and it's a valid number then you have an integer.
5) If there is a decimal, see if there is any number other than zero following it.
6) If there are only zeros, then you have an integer, otherwise it's a float.

It's not as pretty but it would work.

Samuraid
02-07-2006, 07:28 PM
I would personally read the number in as a string and parse the digits to see if they were all numeric and if it had a decimal point (which would mean it was a float not an int). Then one could simply cast it to an int.

EDIT: Ah Mulley you beat me.

Dr Unne
02-07-2006, 11:33 PM
How about testing if x - (int)x == 0.0 .


Well you could always use a brute force approach.

1) Treat the input as a string.
2) Go through the input character by character and verify that each character is only 0-9 or a ".".
3) If there is a ".", check that there is only one ".".
4) If there is no decimal and it's a valid number then you have an integer.
5) If there is a decimal, see if there is any number other than zero following it.
6) If there are only zeros, then you have an integer, otherwise it's a float.

It's not as pretty but it would work.

I think it depends on the definition of "is an integer". If you input 1.0, to store it into an int variable it would have to be cast as an int from a float or double at some point. 1.0 is technically a floating point number, mathematically equal to 1 but maybe not computationally equal. Strictly speaking I wouldn't call 1.0 an integer.

Using your definition above though, 1e2 would also be an integer.

My C is more than a bit rusty (and spoiled from Perl) but I think you can enter integers in straight hexidecimal in C, such as 0x100. Or in binary, 0b1001.

You can also have + or - at the beginning of an integer.

Samuraid
02-08-2006, 01:24 AM
How about testing if x - (int)x == 0.0 .
Great idea. :)

I'm not sure if scanf() accepts all number formats inclusively. You would have to specifically scan for either hex or decimal with the format specifier. If I remember correctly, you have to use something like strtol() to be able to input direct hex/octal numbers as well as numbers with leading + and -.

Flying Mullet
02-08-2006, 01:15 PM
Great idea. :)
Yes, very elegant. :)

Cruise Control
02-08-2006, 04:04 PM
How about testing if x - (int)x == 0.0 .That was brutally efficient, if I wasn't so sure you'd stab me I'd give you a hug.

You helped a friend and I out immensely.

Mr. Wertz
02-08-2006, 04:43 PM
Yes, you helped me alot. Lots of props to you.

Cruise Control
02-14-2006, 04:44 PM
Can someone give me the coordinates to make a Zanarkand Abes Logo. It's my optional shape for a graphics program.

edczxcvbnm
02-14-2006, 05:11 PM
Why don't you print out the logo get some graph paper and plot it out yourself? That is a simple solution.

Note: I have no played FFX thus can't even begin to help with coordinates but I am also very art impared.

Cruise Control
02-14-2006, 09:28 PM
Me too. I don't know where to start.

edczxcvbnm
02-15-2006, 12:10 AM
I could do it if I knew what it looked like but I told you what to do. Print out the logo and get some graph paper and plot the logo. It isn't rocket science. I doubt it has to be perfect. That or do something easier such as the Samus Metroid symbol. It is straight lines and perfect circles.

Cruise Control
02-17-2006, 04:12 PM
I got them and I'll post them later. But now I need an idea of how to use a for loop to check for prime numbers. I don't think I can use an if statement in a loop checking for predertimined values.

EX:
I cant do

for (int cd=0; cd<100; cd++)
{
if (n =2||3||5||7||11||13||17||19||23||29||31||37||41||43||47||53||59||61||67||71||73||79||83||89||97);
cout << I'm sorry, " << n << "is not an integer.";
}


So does anyone have any ideas.

Flying Mullet
02-17-2006, 04:15 PM
This is where Google becomes your best friend. I would suggest searching for examples on the web. I'm sure that someone has done this before.

EDIT: I know it's Java, but the languages are very similar. Try searching at http://forum.java.sun.com/index.jspa. I searched on "prime number" and it returned plenty of results. Perhaps one will be able to help you.

Cruise Control
02-17-2006, 04:20 PM
You can use google for something besides porn! :eek:

EDIT1: I figured it out all by myself.


int main()//Initializes program
{
int cdn; //Countdown start #
int mr1; //Number stored from modulus 2
int mr2; //Number stored from modulus 3
int mr3; //Number stored from modulus 5
int mr4; //Number stored from modulus 7
for (int cdn=1; cdn<101; cdn++)
{
mr1=cdn%2;
mr2=cdn%3;
mr3=cdn%5;
mr4=cdn%7;
if (mr1!=0&&mr2!=0&&mr3!=0&&mr4!=0)
{
cout << cdn << ", ";
outfile << cdn
}
}
getch();
return 0;
}

EDIT2: The coordinates for your Zanarkand Abes Logo are:
moveto (10, 30);
lineto (40, 0);
lineto (60, 10);
lineto (70, 30);
lineto (80, 10);
lineto (100, 0);
lineto (130, 30);
lineto (100, 50);
lineto (110, 30);
lineto (100, 20);
lineto (80, 40);
lineto (80, 50);
lineto (90, 40);
lineto (100, 90);
lineto (70, 140);
lineto (30, 60);
lineto (60, 80);
lineto (50, 80);
lineto (70, 110);
lineto (80, 90);
lineto (60, 50);
lineto (50, 40);
lineto (60, 40);
lineto (60, 30);
lineto (40, 20);
lineto (30, 30);
lineto (40, 50);
lineto (10, 30);
floodfill(11, 30, fg);

Thanks for the graphing idea edczxcvbnm .

Endless
02-17-2006, 11:56 PM
I got them and I'll post them later. But now I need an idea of how to use a for loop to check for prime numbers. I don't think I can use an if statement in a loop checking for predertimined values.
So does anyone have any ideas.

It should be obvious that you are to use what you were asked to do before, which is, to test if a number is an integer.
It's not actual c code, but you should be able to adapt it.

"number" = the number inputed.



bool isprime=true;
if ((number/2)-int(number/2) == 0.0)
{
isprime = false;
}
for (int i=3, i<=int(sqrt(number))+1 && isprime, i+2) /* We only need to test up to the square root for divisions,
since it's symmetrical. Also, if the number is a multiple of 2,
then we don't bother with the loop (isprime == false). */
{
if ((number/i)-int(number/i) == 0.0) /* Is the result of the division an integer? */
{
isprime = false; /* Result is an integer which means that i is a factor of number.
Also exits the loop */
}
}
if (isprime)
{
cout << "The number " << number << " is prime";
} else
{
cout << "The number " << number << " is not prime";
}


Edit: silly me, I forgot to comment. *adds comments to code*
Alternatively, you can test if "number mod i" == 0. If it's the case, it's not prime.

Edit2: Optimized the code to cut by two the number of iterations.

Mr. Wertz
02-20-2006, 04:13 PM
//Bryan Wertz II
//February 10, 2006
//Terminal 1, Intermediate Computer Prgramming.
//Switch Program


//These are my header files.
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>


//This is the start of my program.
int main()
{
//prints to the text file.
ofstream outfile ("PrimeNumberProgram.txt");

//Declaring my variables.
int countdown; //number that starts the countdown.
int remainder2; //number that is stored from modulus 2.
int remainder3; //number that is stored from modulus 3.
int remainder5; //number that is stored from modulus 5.
int remainder7; //number that is stored from modulus 7.

cout << "1,\n2,\n3,\n5,\n7,\n";
outfile << "1,\n2,\n3,\n5,\n7,\n";
// counting to 100 using a for loop.
for (int countdown=11; countdown<101; countdown++)
{
remainder2=countdown%2;
remainder3=countdown%3;
remainder5=countdown%5;
remainder7=countdown%7;
if (remainder2!=0&&remainder3!=0&&remainder5!=0&&remainder7!=0)
{
cout << countdown << ",\n";
outfile << countdown << ",\n";
}
}

//That stuff at the end.
getch();
return 0;
}

Endless
02-20-2006, 05:46 PM
Is your program supposed to give you a list of prime numbers up to 101, or check if a particular number is prime?
If it's the former, testing for mod2 is useless, and your for loop should go from odd number to odd number (even numbers can't be prime, what's the point of testing them?). Also, as long as your loop is below 25, mod5 is useless, and as long as it's below 49, mod7 is useless aswell. And last, were you specifically told to list them up to 101, or did you choose that as a random stopping point?
If it's the latter, my pseudo-program is better. :p In fact, I'm confident I can make a prime number generator using my code that takes any stopping point as input, whereas yours needs to be rewritten each time. :p

Cruise Control
02-23-2006, 04:13 PM
This is now the official thread for intermediate C++ students to help each other.

Dreaper, do you know why this doesn't work, the != doesn't make a difference.


//Chris Taylor
//February 22, 2006
//Terminal 6, Intermediate Computer Prgramming.
//Switch Program


//These are my header files.
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>

//This is the start of my program.
int main()
{
//prints to the text file.
ofstream outfile ("500.txt");

//Declaring my variables.
int cd=5; //number that starts the countdown.
int r1; //number that is stored from division of 5.
int r2; //number that is stored from modulus of r1.
int r3; //number that is stored from division of 7.
int r4; //number that is stored from modulus of r3.
while (cd<500)
{
r1=cd/5;
r2=r1%5;
r3=cd/7;
r4=r3%7;

if (r2==0||r4==0)
cout << cd << ", ";
cd++;
}

//That stuff at the end.
getch();
return 0;
}

EDIT: It works now.


int main()
{
ofstream outfile ("PN.txt");

int cd=5; //number that starts the countdown.
int r1; //number that is stored from modulus of r1.
int r2; //number that is stored from modulus of r3.

while (cd<500)
{
r1=cd%5;
r2=cd%7;

if (r1==0||r2==0)
cout << cd << ", ";
cd++;
}

getch();
return 0;
}

Cruise Control
02-23-2006, 05:08 PM
Since the user is able to enter a value less than, what happens if they enter a max count of less than 5? Our starting value IS 5. We wouldn't want it to run anyway. Not to mention Do While loops are gay.

Mr. Wertz
02-23-2006, 05:09 PM
no idea what that means, dee dee dee.

Cruise Control
02-23-2006, 05:11 PM
x.x Just write it.

Mr. Wertz
02-23-2006, 05:13 PM
that isnt a answer though.

It makes no sense.

Cruise Control
02-23-2006, 05:13 PM
Yes it is, it explains the reasoning.

Endless
02-23-2006, 10:31 PM
Your code doesn't list primes, only multiples of 5 and 7.

Dr Unne
02-23-2006, 11:02 PM
Since the user is able to enter a value less than, what happens if they enter a max count of less than 5? Our starting value IS 5. We wouldn't want it to run anyway. Not to mention Do While loops are gay.

While, do-while, for, etc. are all equally powerful. You can use any of them to construct the equivalent of any of the others. (Generally, depending how a language is implemented.) All loops are just syntactic sugar over a LOOP / GOTO / END construct when it comes down to it.

Since this is apparently a general C++ thread now, I'll offer some other advice. r1, r2, r3 etc. are very bad choices for variable names. If something stores the modulus of 5, call it at bare minimum "modulus_of_5". If something is a countdown variable, "countdown" is much better than "cd". If you named your variables like this, you wouldn't even need most of the comments you have in your code there. We call it "self-documenting".

Comments should never say WHAT your code is doing. If you can't tell what you're doing in the code just by reading the code, the code probably needs to be rewritten or clarified. Comments should say things like 1) Overall purpose / summary of entire blocks or functions 2) WHY you're doing something (not WHAT you're doing or HOW you're doing it), 3) Anything happening that's subtle or not immediately obvious, 4) Assumptions that are being made about input/output, which your code relies upon. Endless has good comments above. Source code is meant for human beings to read. Machines only understand binary. Good code = easy to read code.

Another piece of advice: if something is a constant (modulus of 5, etc) you should make it a const variable in your code. It prevents accidentally changing the value of the variable. Ideally your program should crash any time anything happens that you didn't anticipate; declaring things as constants is a good way to crash your program. It forces you to correct the problem, and makes it easier to find it to debug. Silent bugs are the worst kind.

So far as finding prime numbers, you could always use the Sieve of Eratosthenes. It's a very simple, famous algorithm for finding primes. I just wrote up a C++ version of it, but then I realized everyone should have the opportunity to enjoy writing it themselves.

*wanders off*

Cruise Control
02-24-2006, 01:49 AM
Since this is apparently a general C++ thread now, I'll offer some other advice. r1, r2, r3 etc. are very bad choices for variable names. If something stores the modulus of 5, call it at bare minimum "modulus_of_5". If something is a countdown variable, "countdown" is much better than "cd". If you named your variables like this, you wouldn't even need most of the comments you have in your code there. We call it "self-documenting". I do it, Remainder 1 etc.. the shorter the variable name, the less likely I am to type something wrong.


Comments should never say WHAT your code is doing. If you can't tell what you're doing in the code just by reading the code, the code probably needs to be rewritten or clarified. Comments should say things like 1) Overall purpose / summary of entire blocks or functions 2) WHY you're doing something (not WHAT you're doing or HOW you're doing it), 3) Anything happening that's subtle or not immediately obvious, 4) Assumptions that are being made about input/output, which your code relies upon. Endless has good comments above. Source code is meant for human beings to read. Machines only understand binary. Good code = easy to read code.Our teacher wants us to comment everything.

I can't argue with the rest, I welcome any and all advice, since I'm not good at C++, I just like doing it.

Cruise Control
02-27-2006, 04:20 PM
This is a toughie, we need some help. The prompt is as follows:

"Your program will allow the user to input a "countdown" time including hours, minutes, and seconds, and will accurately count down the time until all units are zeroes. At that time you may print out an appropriate message."

I need tu use a nested loop, and can't use pre defined functions to count. How would I do this?

Samuraid
02-27-2006, 07:38 PM
You cannot generate accurate time without using the kernel to supply you with signals/clock. (that's basically what the time-related functions do)

You could try and simulate a clock with a certain number of loops delay, but it will drastically vary across machines and will also depend on how your program's execution is scheduled.

Cruise Control
02-27-2006, 07:45 PM
Thats what we are doing. But I don't know how to start. I can fine tune it to this peice of crap machine.

Endless
02-27-2006, 08:56 PM
while hours >= 0
{
while minutes >= 0
{
while seconds > 0
{
for i = 1 to whatever_is_needed_to_make_second
{
// Something to pass time here.
}
seconds = seconds - 1;
}
seconds = 60;
minutes = minutes - 1;
}
minutes = 59;
hours = hours -1;
}
cout << "Time's up!";

Flying Mullet
02-27-2006, 09:22 PM
Or convert the time to all seconds and count down against that master number.

-N-
02-27-2006, 09:42 PM
I think C++ has a Sleep command.
It's Sleep(int milliseconds) - I think it's in time.h or windows.h .

Endless
02-27-2006, 09:58 PM
Or convert the time to all seconds and count down against that master number.

That would be simple, wouldn't it? Let's see what the assignment says:


I need tu use a nested loop



I think C++ has a Sleep command.
It's Sleep(int milliseconds) - I think it's in time.h or windows.h .

Similarly...


I (...) can't use pre defined functions to count.

That said, if you can use wait/pause/sleep, use it instead of the for {} loop I used. That's also where you would put a message that displays how much time is left if you wanted.

Dr Unne
02-27-2006, 11:27 PM
I can't see how you could do this at all accurately without using system calls. strftime makes me happy. Even system calls aren't guaranteed to be accurate, depending on your OS's scheduler. (Though the error is likely to be so small you wouldn't notice.)

Also note, some compilers optimze away loops that don't do anything. So if you do something like


for(int c=0; c<5000000000; c++)
{ }

expecting it to take a long time, and it takes 0 seconds to run, that's probably why.

Endless
02-28-2006, 07:14 AM
You could guesstimate a second with:


looptime = time()
while time() < looptime+1000
{ i = i + 1
}


Then note what i was (let's call it i_max), and reuse it as:


while i < i_max+1 // to preserve the time used by that operation
{ i = i + 1
}

Cruise Control
02-28-2006, 10:09 AM
I think what I'm doing is similiar to when you say 1 Mississippi, 2 mississippi.

If I have nested loops, the in side loop runs first. So I make a nested loop, make it count in that one first, then go out, count 1 in the out side, and back to the inside.

The inside loop serves to slow it down. All I need to do is tweak the number in the nested loop.

My teacher was nice, he must have gotten laid recently.

Cruise Control
02-28-2006, 04:08 PM
Like so?
tw=time waster

EDIT: WHY WON'T THIS SoB WORK?!?


#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>

//This is the start of my program.
int main()
{
//prints to the text file.
ofstream outfile ("500.txt");

//Declaring my variables.
int hours;
int minutes;
int seconds;
int tw=1000;
cout << "This program, will function as a countdown 'clock'. Please enter your value for hours, then your value for minutes, followed by your value for seconds.\n";
outfile << "This program, will function as a countdown 'clock'. Please enter the number you would like to countdown from.";
cin >> hours;
cout << "hours";
cin >> minutes;
cout << "minutes";
cin >> seconds;
cout << "seconds";
while (hours >= 0)
{
while (minutes >= 0)
{
while (seconds > 0)
{
while (tw>0)
{
tw--;

if (tw==1)
seconds++;
}
seconds = seconds - 1;
}
seconds=60;
minutes=minutes - 1;
}
minutes = 59;
hours = hours -1;
}
cout << "Time's up!";

//That stuff at the end.
getch();
return 0;
}

Endless
02-28-2006, 04:24 PM
Because your timewaster will only run once. And if even if it ran every second, there's no need for seconds++.

Flying Mullet
02-28-2006, 04:24 PM
You never reinitialize your time waster to 1000.

It would probably be better to treat time waster as a constant and use a for loop and compare i against tw.

Cruise Control
02-28-2006, 04:25 PM
XD Woops.

EDIT: WTF I got an infinite loop.
while (tw>0)
{
tw--;

if (tw==1)
{
seconds--;
cout << hours << ":" << minutes << ":" <<seconds;
tw=tw+999;
}
}

Endless
02-28-2006, 05:00 PM
Well, duh. tw goes 1000, 999, 998 ..., 2, 1, 999, 998, 997..., 2, 1, 999, ... Why is tw=999 <b>inside</b> the while loop?

Cruise Control
02-28-2006, 05:12 PM
TW is initialized to one. But all it does is print out the entered value 60 times.
;______________;

while (hours >= 0)

while (tw<1000)
{
tw++;
cout << hours << ":" << minutes << ":" <<seconds << "\n";

Flying Mullet
02-28-2006, 05:12 PM
And don't forget to re-initialize "tw".



while (seconds > 0)
{
while (tw<1000)
{
tw++;
cout << hours << ":" << minutes << ":" <<seconds << "\n";
}
seconds=seconds-1;
tw = 0;
}

Cruise Control
02-28-2006, 09:59 PM
Thanks to you brilliant gents *checks Endless's profile*, I have it counting properly, and all I need to do is tweak the counter till it's in tune with a clock.

Flying Mullet
02-28-2006, 10:10 PM
After you tweak it for the computer that you are programming on, you should try running the program on a computer that is either slower or faster than it. This will show you how much variance the computer's processing power will have on the time and if it will be noticable if someone else runs it on a different computer.

Cruise Control
02-28-2006, 10:30 PM
I had an infinite loop involving negative time when I first tried it on my own. I thought you'd find that humorous.

Flying Mullet
02-28-2006, 10:41 PM
You've discovered how to go back in time! :eek:

Cruise Control
03-01-2006, 10:47 AM
Wanna borrow it? BACK TO 1982!

Flying Mullet
03-01-2006, 01:20 PM
Push it to the limit!

Cruise Control
03-01-2006, 04:23 PM
Done. I'm off only by .5 of a second every minute, well within the range of 15% error. XD. I'm so uber. I really appreciate all your help.

Mr. Wertz
03-02-2006, 04:30 PM
Im finished with it too.

Cruise Control
03-02-2006, 10:00 PM
PM me instead of spamming, kthxbai.

Mr. Wertz
03-14-2006, 04:58 PM
how does this cube program look bbomber72011.




//Bryan Wertz II
//March 8th, 2006
//Intermediate C++, Terminal 1.
//Cube Program

#include <graphics.h> //Header file.
#include <conio.h> //Header file.
#include <iostream.h> //Header file.
#include <fstream.h> //Header file.

void Cube_Printer() //function that prints a cube on the screen.
{

int graphdriver=DETECT,graphmode; //Goes into Graphics mode.
initgraph(&graphdriver,&graphmode,"c:..\\bgi");

moveto(120,130);
lineto(150,110);
lineto(210,110);
lineto(180,130);
lineto(120,130);
moveto(120,170);
lineto(180,170);
lineto(210,150);
lineto(210,110);
moveto(120,170);
lineto(120,130);
moveto(180,170);
lineto(180,130);
moveto(210,150);
lineto(150,150);
lineto(120,170);
moveto(150,150);
lineto(150,110);

setbkcolor(1);
setlinestyle(0,0,3);
setcolor(3);

rectangle(120,130,180,170);
moveto(120,130);
lineto(150,110);
lineto(210,110);
lineto(180,130);
moveto(210,110);
lineto(210,150);
lineto(180,170);

setlinestyle(1,0,3);
setcolor(3);

moveto(150,110);
lineto(150,150);
lineto(120,170);
moveto(150,150);
lineto(210,150);
moveto(300,300);
outtext("Press any key to end.");

getch();
closegraph(); //closes graph.
}

int main()
{
cout << "This is going to print a cube onto the screen.\n\n\n";
cout << " Press any key to continue!";
getch();

Cube_Printer(); //call to Cube_Printer.
return 0; //end of program.
}

Cruise Control
03-14-2006, 05:00 PM
IT SUCKS!

Nice job, it runs.

Mr. Wertz
03-14-2006, 05:05 PM
it doesnt suck.

and if it does, then it doesnt matter because I found it anyway. LAWLZ.

Cruise Control
03-23-2006, 04:27 PM
I need help, how in blaspheme do I pass data? Can I get an example

float c(float fa, float ce);
float f(float fa, float ce);

int main()
{
float choice; //Stored for intial choice.

cout << "This will convert fahrenheit to celsius.";
cout << "What do you want to start with, enter your number than print enter.\n";
cout << " 1. Fahrenheit\n";
cout << " 2. Celsius\n";
cin >> choice;

if (choice==1)
f();
else
c();
return 0;
}

float c(float fa, float ce)
{
float fa; //Fahrenheit variable.
float ce; //Celsius variable.

cout << "Input your fahrenheit vaulue and press\nENTER.\n";
cin >> ce;
fa=1.8*ce+32;
}

float f(float fa, float ce)
{
float fa; //Fahrenheit variable.
float ce; //Celsius variable.
cout << "Input your celsius vaulue and press\nENTER.\n";
cin >> fa;
ce=(fa-32)/1.8;

}

Flying Mullet
03-23-2006, 04:43 PM
Do you mean use parameters/arguments in your functions?

Cruise Control
03-23-2006, 04:46 PM
I guess *updates code*

Flying Mullet
03-23-2006, 04:54 PM
There are two ways of passing arguments to a function, by value or by reference.

To pass by value, you simply name the variables in the function definition:


void functionByValue(int a, float b)
{
// yada yada yada
}

What this does is make a copy of the variables passed to the function and uses them locally, hence, by value.

The other way to pass variables is by reference, which looks like this:


void functionByValue(int a&, float b&)
{
// yada yada yada
}

What this does is pass a reference (pointer) to the "calling" variable, rather than copy it's value. This means that if you change the value of a to 2 in the function, the original a will be changed to 2. This is useful when you want to return more than one value from a function.

You can also mix and match by reference and by value:


void functionByValue(int a&, float b)
{
// yada yada yada
}

In this case a local copy of b will be made and a will point to the "calling" a.

Hope this helps.

<b>EDIT:</b> Also, I don't know what book(s) you are using in class, but I used <a href="http://www.amazon.com/gp/product/0672323087/qid=1143132976/sr=2-1/ref=pd_bbs_b_2_1/104-9506222-3431117?s=books&v=glance&n=283155">Object-Oriented Programming in C++ by Robert Lafore</a> and it makes learning C++ a lot easier. Granted, I have the 2nd edition (was the current one when I was in school) and the current one is the 4th edition. But regardless, it's a great book and I highly reccomend it.

Endless
03-23-2006, 06:16 PM
float c(float fa, float ce);
float f(float fa, float ce);

int main()
{
float choice; //Stored for intial choice.

cout << "This will convert fahrenheit to celsius.";
cout << "What do you want to start with, enter your number than print enter.\n";
cout << " 1. Fahrenheit\n";
cout << " 2. Celsius\n";
cin >> choice;

if (choice==1)
f();
else
c();
return 0;
}

float c(float fa, float ce)
{
float fa; //Fahrenheit variable.
float ce; //Celsius variable.

cout << "Input your fahrenheit vaulue and press\nENTER.\n";
cin >> ce;
fa=1.8*ce+32;
}

float f(float fa, float ce)
{
float fa; //Fahrenheit variable.
float ce; //Celsius variable.
cout << "Input your celsius vaulue and press\nENTER.\n";
cin >> fa;
ce=(fa-32)/1.8;

}

Your code makes baby Jesus cry. And I still don't get why some teachers make students use C++ for C programming. Srsly. There's no object in your code, ever.
Anyway.
Why is f defined twice?
Why is c defined twice?
Why did you define f and c to use two parameters, yet redefined fa and
ce inside?
Why did you define f and c to use two parameters and never passed any?
Why define a function with two arguments that only uses one and should return one that you don't even use?
Did I mention the awful naming of variables? Do you really need both fa and ce?

o_O
03-25-2006, 06:00 AM
int main() {
float choice;
cout << "This will convert fahrenheit to celsius.";
cout << "What do you want to start with, enter your number than print enter.\n";
cout << " 1. Fahrenheit\n";
cout << " 2. Celsius\n";
cin >> choice;

if (choice == 1) {
float fTemp;
cout << "Input temperature in fahrenheit: ";
cin >> fTemp;
cout << toCelcius(fTemp);
else {
float cTemp;
cout << "Input temperature in celcius: ";
cin >> cTemp;
cout << toFahrenheit(cTemp);
}
}

float toCelcius(float fTemp)
{
return (fTemp-32)/1.8
}

float toFahrenheit(float cTemp);
{
return (1.8*cTemp)+32;
}


This could be one way to do it by passing variables. You need to define the variable outside of the function in order to pass it too the function.

Endless
03-25-2006, 08:38 AM
You also need to define functions <i>before</i> calling them. :p
Also, one function is enough to do the conversion.

o_O
03-25-2006, 10:51 PM
Shh, I'm a Java programmer! :p
>_>
<_<
I was trying to preserve the original layout too. :p

I guess this would be a better program then.



float convert(float temp, bool toFahrenheit);

int main() {
int choice;
cout << "Choose your conversion:\n1. To fahrenheit\n2. To celcius";
cin >> choice;
bool toFahrenheit = (choice == 1);
cout << "Enter your temperature: ";
cin >> temp;
cout << convert(temp, toFahrenheit);
}

float convert(float temp, bool toFahrenheit)
{
if (toFahrenheit) return (1.8*temp)+32;
else return (temp-32)/1.8;
}


:p

Endless
03-25-2006, 11:35 PM
Close. It won't compile because you forgot to declare a variable :p
Why make a boolean when you can pass choice directly? Also, you need a couple \n here and there so that it displays better.
I mean:


<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font> ./conv2
Choose your conversion:
1. To fahrenheit
2. To celcius1
Enter your temperature: 12
53.6<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font> ./conv2
Choose your conversion:
1. To fahrenheit
2. To celcius2
Enter your temperature: 53.6
12<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font>

o_O
03-26-2006, 01:28 AM
xD
So I did. I guess that's what I get for not testing it before I post it. :p

Cruise Control
03-27-2006, 08:50 PM
Alrightm I finished but it looks totally different. I need to write a function that will round any given number to the 10th decimal place.



//Chris Taylor
//March 15th, 2006
//Intermediate C++, Terminal 6.
//Cube Thingy

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

//Prototype statements
void pdescription();
int choice (int choose);
float c(float fa);
float f(float ce);
void output(float fa, float ce);

int main()
{
//Variable declaration
float fa=32; //Fahrenheit variable.
float ce=0; //Celsius variable.
int choose=0; //Stored for intial choice.

pdescription();//Calls the function "description"
choice (choose);//Calls the function "choice"

if (choose==0)//Determines what is being converted to what.
{
f(ce);
}

else
{
c(fa);
}

output(fa, ce);

getch();
return 0;
}

void pdescription()//Prints out program description
{
cout << "What do you want to start with, enter your number than print enter.\n";
}


int choice(int choose)//Allows user to choose what is being converted to.
{
cout << " 0. Fahrenheit\n";
cout << " 1. Celsius\n";
cin >> choose;
return choose;
}

float c(float fa)//For converting into fahrenheit.
{
float ce;
cout << "Input your celsius vaulue and press\nENTER.\n";
cin >> ce;
fa=(1.8*ce)+32; //Formula to convert c to f.
return fa;
}

float f(float ce)//For converting into celsius.
{
float fa;
cout << "Input your fahrenheit vaulue and press\nENTER.\n";
cin >> fa;
ce=(fa-32)*(5.0/9.0);//Formula to convert f to c.
return ce;
}

void output(float fa, float ce)//Prints fahrenheit and celsius value to the screen.
{
clrscr();
cout << "Fahrenheit value is: " << fa << endl;
cout << "Celsius value is: " << ce;
}

Endless
03-27-2006, 09:19 PM
I prefer my version that's half as long and does the same xD

And for displaying a fixed number of decimals:
#include &lt;iomanip>

Then just before the cout (only the first time):
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(number_of_decimals_here);


Or for rounding (warning, this comes from the web, in case your teacher checks):

float Round(float Value, int NumPlaces)
{
int k, Temp;
float Factor;

Factor = 1;
for (k = 0; k < NumPlaces; k++)
Factor = Factor * 10;

Temp = Value * Factor + 0.5;
return Temp / Factor;
}

Dr Unne
03-27-2006, 09:37 PM
Alrightm I finished but it looks totally different. I need to write a function that will round any given number to the 10th decimal place.

Is <strong>printf("%.10f", var);</strong> cheating?

Cruise Control
03-28-2006, 03:56 PM
My code is lengthy due to the program description.


//Chris Taylor
//March 15th, 2006
//Intermediate C++, Terminal 6.
//Cube Thingy

#include <iostream.h>
#include <fstream.h>
#include <conio.h>


//Prototype dstatements
void pdescription();
int choice (int choose);
float c(float fa);
float f(float ce);
void output(float fa, float ce);

int main()
{
//Variable declaration
float fa; //Fahrenheit variable.
float ce; //Celsius variable.
int choose=0; //Stored for intial choice.

pdescription();//Calls the function "description"
choice (choose);//Calls the function "choice"

if (choose==0)//Determines what is being converted to what.
{
f(ce);
}

else
{
c(fa);
}

output(fa, ce);

getch();
return 0;
}

void pdescription()//Prints out program description
{
cout << "This program will print out two random value for triangle legs, determine the hypotenuse, and quiz the user on the answer.\n";
}


int choice(int choose)//Allows user to choose what is being converted to.
{
cout << " 0. Fahrenheit\n";
cout << " 1. Celsius\n";
cin >> choose;
return choose;
}

float c(float fa)//For converting into fahrenheit.
{
float ce=0;
cout << "Input your celsius vaulue and press\nENTER.\n";
cin >> ce;
fa=(1.8*ce)+32; //Formula to convert c to f.
return fa;
}

float f(float ce)//For converting into celsius.
{
float fa=32;
cout << "Input your fahrenheit vaulue and press\nENTER.\n";
cin >> fa;
ce=(fa-32)*(5.0/9.0);//Formula to convert f to c.
return ce;
}

void output(float fa, float ce)//Prints fahrenheit and celsius value to the screen.
{
clrscr();
cout << "Fahrenheit value is: " << fa << endl;
cout << "Celsius value is: " << ce;

}

Endless
03-28-2006, 04:28 PM
Did you actually test your code?

Cruise Control
03-28-2006, 04:32 PM
Yeah. Sort of. I'm bad at this.

Header files don't show up when you post code.

Endless
03-28-2006, 04:36 PM
I removed the getch and clrscr functions as they are nonstandard and prevent compilation. Notice how no matter what my choice is and the temperature entered, the result is the same?

<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font> ./conv3
What do you want to start with, enter your number than print enter.
0. Fahrenheit
1. Celsius
0
Input your fahrenheit vaulue and press
ENTER.
10
Fahrenheit value is: 32
Celsius value is: 0<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font> ./conv3
What do you want to start with, enter your number than print enter.
0. Fahrenheit
1. Celsius
1
Input your fahrenheit vaulue and press
ENTER.
10
Fahrenheit value is: 32
Celsius value is: 0<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font>

Cruise Control
03-28-2006, 04:41 PM
I got that, I thought I fixed it.

Cruise Control
03-30-2006, 04:34 PM
I need a rounding function to round hyp to the 10th decimal place.

Endless
03-30-2006, 09:23 PM
Or for rounding (warning, this comes from the web, in case your teacher checks):

float Round(float Value, int NumPlaces)
{
int k, Temp;
float Factor;

Factor = 1;
for (k = 0; k < NumPlaces; k++)
Factor = Factor * 10;

Temp = Value * Factor + 0.5;
return Temp / Factor;
}

Cruise Control
04-05-2006, 04:41 PM
Yeah, I kinda got expelled, so none of this mattered. I apreciate it so much though, you have broadened my understanding of C++. Thank you.

Flying Mullet
04-05-2006, 04:42 PM
You got expelled? :(

bipper
04-05-2006, 05:18 PM
Yeah, I kinda got expelled, so none of this mattered. I apreciate it so much though, you have broadened my understanding of C++. Thank you.

ZMGwtfHOLYHEL wat? You call in a Bbomb threat or somthing? It does matter bomber, you matter. Either way you learned! They can give you a grade, but only you can give yourself ... ugh.. **somthing sedimental and deep.**

Bipper

Cruise Control
04-05-2006, 05:22 PM
Um, I kinda failed. It doesn't matter, since I'm dropping out of school, getting a GED, and and going to a buisness/tech college that takes any age and GED's. I will then use that transcript for future colleges.

I accidently stabbed somebody.

bipper
04-05-2006, 06:30 PM
Accidentally? Stabbed...

...... aukward.

You can still get your diploma, by the way. I would go that route, even though a GED seems the same - it will always get that negative connotation. There is nothing wrong with a tech or trade degree. They make it a bit harder to get your foot in the door, but you are also a LOT more specialised in one single area.

I went to a Technical College for Programming, and once I got in the door, I flew up to where I am now.

I look at it like this. Normal universities study theory (etc), tech teaches you how to advance and work hands on (in most cases). Well, I would rather learn to work, than theorhetically work - right?? :)

Whatever you do man, best of luck to ya,

Bipper

Cruise Control
04-05-2006, 08:11 PM
I'm also planning on using my transcript from this college to get into others that won't take a GED.

BTW, my police statement of what happened is attached.

bipper
04-05-2006, 08:19 PM
LOL, you can't be expelled for that. I mean.. what the hell. SUE! SUE!
I hate over zealous authority figures that have to fuck with peoples fricken lives to show their prowress. To hell with him. You really should fight this, assuming your repoort is accurate. IS the other kid pressing charges? Did he ever buy Kingdom hearts 2?

Bipper

Cruise Control
04-05-2006, 08:21 PM
XD Yes, I saw him on my way out convincing his parents it was awesome.

They neglected to give me an expulsion hearing, and my mom is sueing.
He can't press charges, they made him drop them, because he attacked first.