Recruitment
Raid Times
Site Contributions
1
 
Post new topic   Reply to topic    <Warcraftier> Forum Index -> Fun Stuff
View previous topic :: View next topic   Goto page 1, 2, 3  Next
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Hey LG, I got my rounding script down, but I forgot how to make it into a function. Do I define it after the code, or before? Heres what ive got.

Quote:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){

float number;
cout<<"Enter number to be rounded." << '\n';
cin>> number;
double num = number;
cout << setprecision(2) << fixed << num << '\n';
return 0;
}
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
either or, as long as it is in the namespace. in main you'll call it.


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Dont have access to VisualStudio atm but this is what I came up with.

Quote:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){

float setpres;
float number;

cout<<"Enter number to be rounded." << '\n';
cin>> number;
double num = number;
cout << setpres << '\n';
return 0;
}

float setpres
{
setprecision(2) << fixed << num <<
}
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
Not quite...It's been a while with C++, mostly use assembly and proprietary now, but I don't think the inputted number will be successfully passed to the function.

Dirtiest and easiest would be to take your content in main, and post it all in a separate function, set the return type to void and just have the function do all your input/output. All main will be is two lines: call the function, return 0.


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Edit: Nevermind, finished it:

Quote:
#include <iostream>
#include <iomanip>
using namespace std;

float setprec();

int main(){

setprec();
return 0;
}

float setprec()
{
float number;
cout<<"Enter number to be rounded." << '\n';
cin>> number;
double num = number;
cout << setprecision(2) << fixed << num << '\n';
return 0;
}
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
There ya go.

Man...I really need to start programming in C++ again...need to pick up some ATOmegas


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Royally stuck here. How do I convert a char into a function call?

Quote:

//Joshua Draper
//10/3/10
//Water Bill

#include <iostream>
using namespace std;

void codeh();
void codec();
void codei();
float total;
const float hconst=5;
const float cconst=1000;



int main(){

int accnt;
char code;
char galchar;
int gal



cout<<"Please enter account number.";
cin>>accnt;
cout<<"Please enter gallons used.";
cin>>galchar;
gal = atof(galchar);
cout<<"Please enter code.";
cin>>code;
if (code=codeh)
codeh();
if (code=codec)
codec();
if (code=codei)
codei();
return0;
}

char codeh(){
hconst=5
hconst+(gal*.0005)= total
cout<<"Your bill is $">>total>> /n;
return 0;
}

char codec(){
cconst=1000
cconst+(if gal>4000 then gal*.00025)
cout<<"Your bill is $">>total>> /n;
return 0;
}

char codei(){
if gal<4000000 then total=1000
else if 10000000>gal>4000000 then total=2000
else if gal>10000000 then total=3000
cout<<"Your bill is $">>total>> /n;
return 0;
}


Last edited by Jacoste on 2010/10/05 17:39; edited 1 time in total
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
Just some cleaning up first...but are you writing in pseudo-code? Also I'm going to assume you haven't learned about for loops and switch-case.

You intially declare the functions as void, but when you actually create the function, you are declaring it as an int. They will need to be the same, void will work since you don't need to return any values to the main function. You will not, however, declare the arguments in the top namespace.

In C++, = is an assignment, even if it is in an if statement, == is an operator to test for equality.

In the codes, the assignee is always to the left of the =. Think of 'x = y' as I want x to have the same value as y.

The if statements in C++ need {} placed appropriately and use 'else'
if (this is true) {
###true code###
}
else {
###false code###
}

The way you currently have it written, it will perform the function in the if statement...ie code=codeh() will assign 0 to code. 0 in C++ is false, thus none of your if statements will run because all your functions are type int, returning 0 then assigning 0 to code.

I'm going to assume that you are expecting an "h", "c", or an "i" as input, so you can put those directly into your if statement: if ( code == "h" || "H" )

Next, to pass those arguments to your functions, you will have to rewrite them to include the arguments.

void codeh(float gal, int accnt){
###code###
}

In the above, you can then use gal and accnt naturally by keeping them the same name. if you go: codeh(float x, int y) then you would use cout << x & y

One more thing you could do to make it a bit fancy, before you return 0, put in an else to the last statement and cout << "Invalid code input" because if someone enters something unexpected, it will just terminate without any warning.

tl;dr: to Answer the question asked,

arguments in your functions.
int funct(int x, int y){
variable xinfunction;
variable yinfunction;
return xinfunction+yinfunction;
}
something = 2
another = 3
cout << funct(something, another) //will display 5

or for you,
void funct(char x, char y){
cout << x << " " << y
return;
}

something = "a"
another = "b"
funct(something, another)

will call funct, assign something to x, another to y and display "a b"

hope this helps.


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Edited, but not done. Have to change around some = and ==, aswell as some other things.
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
And function calls, and the return type, you're returning an int, butdefining the function as a char. The function type only depends on what you are trying to return. If you aren't using the keyword 'return' to give something back to the function that called it (in this case main) then you don't need to have a return type.

And function arguments, let me know when you think ya got it.

and pseudo code or not?


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Couldn't tell you its is psuedo or not.
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Grrr atof wont work for some reason:

Quote:

//Joshua Draper
//10/3/10
//Water Bill

#include <iostream>
using namespace std;

void codeh();
void codec();
void codei();
float total;
const float hconst=5;
const float cconst=1000;



int main(){

int accnt;
char codechar;
int code;
char galchar;
int gal;

cout<<"Please enter account number.";
cin>>accnt;
cout<<"Please enter gallons used.";
cin>>galchar;
gal = atof(galchar);
cout<<"Please enter code.";
cin>>codechar;
code = atof (codechar);
if (code=codeh)
codeh();
if (code=codec)
codec();
if (code=codei)
codei();
}

char codeh(){
hconst=5
hconst+(gal*.0005)== total
cout<<"Your bill is $">>total>> /n;
return 0;
}

char codec(){
cconst=1000
if gal>4000;
cconst+(gal*.00025);
cout<<"Your bill is $">>total>> /n;
return 0;
}

char codei(){
if gal<4000000 then total=1000
else if 10000000>gal>4000000
total==2000;
else if gal>10000000
total==3000;
cout<<"Your bill is $">>total>> /n;
return 0;
}
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
why are you making this hard?

You don't need to use atof(), and the reason it won't work is because it is in a different "library" (can't remember the actual name...but the same object as <iostream>)

Some of your = operators are backwards...
= is an assignment and results in the variable getting a new value (something is now something else)
== is a comparator and results in a boolean value (true or false) (is something equal to something else?)

I have to run right now, but I'll post back...quite a bit more work to do.

But I'll leave ya with some hints:
1) you don't need to use atof
2) your if statements are still non-functional (you're writing them as a half-breed of VB and C++)
3) Cout is << cin is >>

if you want to talk on mumble one day, just grab me from sc2 or something....ps, add me as real-id bro.


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Jacoste
Raider

user avatar

Joined: 18 Jan 2010
Posts: 479

Send private message
Reply with quote

re: c++ Fun

0
Done! My Dad is a beast at c++ so I had him help/explain. Here is the finished code:
Quote:
//Joshua Draper
//10/3/10
//Water Bill

#include <iostream>
using namespace std;

void codeh();
void codec();
void codei();
float total;
const float hconst=5;
const float cconst=1000;

int gal;


int main(){

int accnt;
char codechar[1024];
int code;
char galchar[100];

cout<<"Please enter account number:"<< "\n";
cin>>accnt;
cout<<"Please enter gallons used:"<< "\n";
cin>>galchar;
gal = atof(galchar);
cout<<"Please enter code:"<< "\n";
cin>>codechar;
if (strcmp (codechar, "codeh")==0)
codeh();
else if (strcmp (codechar, "codec")==0)
codec();
else if (strcmp (codechar, "codei")==0)
codei();
return 0;
}

void codeh(){
total=hconst+(gal*.0005);
cout<<"Your bill is: $"<<total<< "\n";
}

void codec(){
if( gal>4000)
total=cconst+(gal*.00025);
else total=1000;
cout<<"Your bill is: $"<<total<< "\n";
}

void codei(){
if( gal<4000000) total=1000;
else if( gal<10000000 && gal>4000000 )
total=2000;
else if( gal>=10000000 )
total=3000;
cout<<"Your bill is: $"<<total<< "\n";
}


Thanks for the help Zeere. envy
Zeere

user avatar

Joined: 19 Jul 2008
Posts:

Send private message
Reply with quote

re: c++ Fun

0
Heh, you're welcome. Quite a bit different then how I would do it, but it works which is the important thing.

Guess I was wrong about the atof...really thought it was in a different 'library'.


_________________
rub a dub dub three nuns in a tub and they're doing it with the garden gnome
Posts from:   
Post new topic   Reply to topic    <Warcraftier> Forum Index -> Fun Stuff All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 
 
User Recognition
Birthdays
user avatar
Verogoth
May. 1st
 
Last Forum PostsLast Forum Post RSS Feed
Server Status
Who's Online
1