News:

Facebook killed the radio star. And by radio star, I mean the premise of distributed forums around the internet. And that got got by Instagram/SnapChat. And that got got by TikTok. Where the fuck is the internet we once knew?

Main Menu

If statments [C++]

Started by AntiVirus, September 23, 2005, 04:56:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AntiVirus

I have a question about an if statement. 

For example:

cout <<"What is your favorite icecream?"<<endl;
cin >> icecream; // this would already be declared up above
if (icecream = strawberry)
{
cout <<"Really, how many servings do you eat per day?"<<endl;
cin >> servings; // this would be declared up above
if (servings = alot) // This is where I get confused, and I am pretty sure I am doing it wrong.
cout >> "Your fat">>endl;
if (servings = small)
cout >> "Good">>endl;
}

return 0;

}


So if they eat strawberry icecream, I want to know how much they eat per day and after that say something, such as "your fat" if it's a lot.  Or maybe if it isn't a lot, and the serving = small then I want it to say something else.  That way my program can respond to multiple things.  But, I don't know how to add that in there.  Because I would also add something for if they pick chocolate or another favor, saying other things..

Right now I don't know how to explain this, if you don't understand please tell me.  I will try and come up with a better way of explaining things.  I am not actually writting a program about icecream, but about something else.  I can show you my code so far, but it isn't all that great.  I am still new to C++
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Sidoh

#1
= != == :P

= - Used to assign a variable
== - Logic statements.

To elaborate, you're going to probably want to use nested-if statements to solve this proglem.  In example:


  if(serving == large) {
    if(flavor == 'chocolate') {
      cout << "Fatass." << endl;
    }
  }


You get the idea.

AntiVirus

#2
What if it's like
if (icecream == strawberry) {
if (serving == alot) {
cout <<"Fatass"<<endl;

That is what I am going for.  But, how is the program suppose to know if the serving is a lot?  Don't I need to ask them?

So would it go:

if (icecream == strawberry){
cout<< "How much do you eat?"<<endl;
cin >> serving;
if (serving == alot){
cout<<"Fatass"<<endl;
}
}

I did it like that, but all I get is The questoin "How much do you eat"  Then I type in how much, then it tells me to press any key to continue... So, it completly ignores my second if.
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Blaze


#define FatAss 3

if (icecream == strawberry){
    cout<< "How much do you eat?"<<endl;
    cin >> serving;
    if (serving >= FatAss){
          cout<<"Fatass"<<endl;
    }
}
And like a fool I believed myself, and thought I was somebody else...

Sidoh

Quote from: Blaze on September 23, 2005, 05:11:13 PM

#define FatAss 3

if (icecream == strawberry){
    cout<< "How much do you eat?"<<endl;
    cin >> serving;
    if (serving >= FatAss){
          cout<<"Fatass"<<endl;
    }
}


Defining constants like that is a complete waste of time, IMO.  (In situations like this, anyway).

AntiVirus

Hrmm.. I think that would work blaze.  But, I don't know how I would do it with what I am workign with.

I will be more spacific.  I am making some little program to convert things.  Such as, convert bytes into killobytes.  So, it would ask "What do you want to convert" and you will type "bytes" then it would say "what do you want to convert it into" then you would say "kilobytes" then it would tell you how many bytes are in a kilo byte.  Not really converstion, but it doesn't really matter to me what I call it.

This is my code:



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

char filetype;
char change;
float byte = 8;
float kilobyte = 1024;
float megabyte = 1048576;
float gigabyte = 1072741824;

cout <<"This program is designed to convert byte, kilobyte, megabyte, and gigabyte into different file sizes." << endl;
cout <<endl;
cout <<"What file size are you wanting to be converted?"<<endl;
cin  >>filetype;
if (filetype==byte) {
cout <<"What do you want to change it into?"<<endl;
cin >> change;
if (change == kilobyte) {
cout << "1024 bytes in a Kilobyte"<<endl;
}
}
return 0;



}


Does that help?

I think I am doing a lot wrong..
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Sidoh

You'll want to use loops with this.  Here's some PHP code I wrote a while ago.  Don't try to translate it into C++, just get the idea of how it works and then start coding in C++.

function get_filesize($s) {

$ut = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'XB');

$i = 0;

while($s >= 1024) {
$s /= 1024;

$i++;
}

$fs = round($s, 2) . ' ' . $ut[$i];

return $fs;
}

Blaze

Quote from: Sidoh on September 23, 2005, 05:14:54 PM
Defining constants like that is a complete waste of time, IMO.  (In situations like this, anyway).

In that instance, your right, but when you start using it all over the place... it becomes eaiser to update.
And like a fool I believed myself, and thought I was somebody else...

Sidoh

Quote from: Blaze on September 23, 2005, 05:22:34 PM
In that instance, your right, but when you start using it all over the place... it becomes eaiser to update.
In a few cases, yes.  Other times it's used so some logic statement can be identified.

A common example of this is the constants that are usually defined in packet buffers that define packet ID's.  It's easier to eye a section in a packet buffer when the ID's are actual names instead of numbers.

iago

Quote from: Sidoh on September 23, 2005, 05:27:26 PM
Quote from: Blaze on September 23, 2005, 05:22:34 PM
In that instance, your right, but when you start using it all over the place... it becomes eaiser to update.
In a few cases, yes.  Other times it's used so some logic statement can be identified.

A common example of this is the constants that are usually defined in packet buffers that define packet ID's.  It's easier to eye a section in a packet buffer when the ID's are actual names instead of numbers.

You should *never* have hardcoded numbers ("magic numbers") in code, constants should be used for everything.  If you're coding a personal project, it might be ok, but if you *ever* plan to code for a team, they'll shoot you if you hardcode a number.  It's a very, very bad habit.  But if you want to continue that discussion, let's start a new thread. 

Joe

1)  You spelled "a lot" wrong.
2) See Sidoh's comment about logic/assignment.
3) Tab your code buddy!
4) Heres the code, tabbed and fixed (read comments for more stuff)

  cout << "What is your favorite icecream?" << endl; // ew at not using spaces there =p
  cin >> icecream;
  if (icecream == "strawberry") { // You want that to be in quotes, because your using a string constant instead of a defined constant
    cout << "Really, how many servings do you eat per day?" << endl;
    cin >> servings;
    if (servings == "alot") {
      // Always use brackets. They keep stuff in line and solve a lot of problems =)
      // Also, see my above comment about string constants
      cout >> "Your fat" >> endl;
    } elseif(servings == "small") {
      // You could simply change this to an else if you wanted, but this allows you to add others, such as medium.
      // Again, see my comment about string constants.
      cout >> "Good" >> endl;
    }
  }
  return 0;
}
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Sidoh

Quote from: iago on September 23, 2005, 06:12:45 PM
You should *never* have hardcoded numbers ("magic numbers") in code, constants should be used for everything.  If you're coding a personal project, it might be ok, but if you *ever* plan to code for a team, they'll shoot you if you hardcode a number.  It's a very, very bad habit.  But if you want to continue that discussion, let's start a new thread. 

I know.  Most of my programming projects are personal projects.  Besides, I use constants most of the time (especially since I so rarely have the chance to hardcode an integer) -- especially when I deem them necissary.  I'm not an idjit, lago :P

iago

Quote from: Sidoh on September 23, 2005, 06:49:30 PM
Quote from: iago on September 23, 2005, 06:12:45 PM
You should *never* have hardcoded numbers ("magic numbers") in code, constants should be used for everything.  If you're coding a personal project, it might be ok, but if you *ever* plan to code for a team, they'll shoot you if you hardcode a number.  It's a very, very bad habit.  But if you want to continue that discussion, let's start a new thread. 

I know.  Most of my programming projects are personal projects.  Besides, I use constants most of the time (especially since I so rarely have the chance to hardcode an integer) -- especially when I deem them necissary.  I'm not an idjit, lago :P

Then don't discourage good programming practices :P

Sidoh

Quote from: iago on September 23, 2005, 06:54:14 PM
Then don't discourage good programming practices :P

While this was an important point to make, it is still useless (and a waste of time) when you only have 70 lines of code.

iago

Quote from: Sidoh on September 23, 2005, 07:07:13 PM
While this was an important point to make, it is still useless (and a waste of time) when you only have 70 lines of code.

But it's a bad habit, and something that somebody learning C should be doing right away.