Author Topic: If statments [C++]  (Read 13094 times)

0 Members and 2 Guests are viewing this topic.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
If statments [C++]
« on: September 23, 2005, 04:56:01 pm »
I have a question about an if statement. 

For example:
Code: [Select]
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

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #1 on: September 23, 2005, 04:57:39 pm »
= != == :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:

Code: [Select]
  if(serving == large) {
    if(flavor == 'chocolate') {
      cout << "Fatass." << endl;
    }
  }

You get the idea.
« Last Edit: September 23, 2005, 05:00:03 pm by Sidoh »

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: If statments [C++]
« Reply #2 on: September 23, 2005, 05:09:48 pm »
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.
« Last Edit: September 23, 2005, 05:11:52 pm by Brandon »
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

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: If statments [C++]
« Reply #3 on: September 23, 2005, 05:11:13 pm »
Code: [Select]
#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...

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #4 on: September 23, 2005, 05:14:54 pm »
Code: [Select]
#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).

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: If statments [C++]
« Reply #5 on: September 23, 2005, 05:15:23 pm »
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:

Code: [Select]

#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

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #6 on: September 23, 2005, 05:18:32 pm »
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++.

Code: [Select]
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;
}

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: If statments [C++]
« Reply #7 on: September 23, 2005, 05:22:34 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...

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #8 on: September 23, 2005, 05:27:26 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: If statments [C++]
« Reply #9 on: September 23, 2005, 06:12:45 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. 

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: If statments [C++]
« Reply #10 on: September 23, 2005, 06:24:14 pm »
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)

Code: [Select]
  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;
}
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #11 on: September 23, 2005, 06:49:30 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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: If statments [C++]
« Reply #12 on: September 23, 2005, 06:54:14 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

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #13 on: September 23, 2005, 07:07:13 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: If statments [C++]
« Reply #14 on: September 24, 2005, 11:55:32 am »
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.