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

0 Members and 1 Guest 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. 

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #15 on: September 24, 2005, 12:41:20 pm »
But it's a bad habit, and something that somebody learning C should be doing right away. 
I think wasting time is more of a bad habbit, but whatever floats your boat. ^^

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: If statments [C++]
« Reply #16 on: September 24, 2005, 01:09:02 pm »
When I put  } elseif(change == "kilobyte") {

Then I get this error.
Quote
error C2065: 'elseif' : undeclared identifier

Here is my code again:
Code: [Select]
#include <iostream>
using namespace std;
int main()
{

char filetype;
char change[10];
float byte = 8;
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;
}
elseif(change == "kilobyte");  {
cout << "1024 bytes in a Kilobyte"<<endl;
}


 return 0;



}

I am confused. :(
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 #17 on: September 24, 2005, 01:09:47 pm »
There isn't an elseif in C++, that I know of..
And like a fool I believed myself, and thought I was somebody else...

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: If statments [C++]
« Reply #18 on: September 24, 2005, 01:15:08 pm »
That's what I thought, but Joe seems to think there is.
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;
}
« Last Edit: September 24, 2005, 01:16:44 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 Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: If statments [C++]
« Reply #19 on: September 24, 2005, 01:21:53 pm »
That's what I thought, but Joe seems to think there is.

Joe is a VB nub (jk joe)

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: If statments [C++]
« Reply #20 on: September 24, 2005, 01:27:55 pm »
Try using Select instead.
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 #21 on: September 24, 2005, 01:28:46 pm »
Try using Select instead.
That wouldn't be logical in this situation.  Besides, C doesn't have Select, it has switch.  :)

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: If statments [C++]
« Reply #22 on: September 24, 2005, 01:31:19 pm »
Ugh... shh!
And like a fool I believed myself, and thought I was somebody else...

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: If statments [C++]
« Reply #23 on: September 24, 2005, 01:32:53 pm »
There is a "else if (blah = blah) thing.  Just elseif can't be together.
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 #24 on: September 24, 2005, 01:35:55 pm »
Ugh... shh!
lol

There is a "else if (blah = blah) thing. Just elseif can't be together.
That's not really a special operator, though.  doing:

Code: [Select]
if(0) { ... } else if(1) { ... } is the same thing as:

Code: [Select]
if(0) {
   ...
} else {
   if(1) {
      ...
   }
}

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: If statments [C++]
« Reply #25 on: September 24, 2005, 04:31:51 pm »
Quit being mean to me because I don't know the difference between C and C++. =p

Anyhow, I almost fixed it again. I tested it too.
Code: [Select]
#include <iostream>
using namespace std;

int main() { //I always put the start bracket on the same line, oh well. =)
  char filetype;
  char change[10];
 
  float byte = 8;
  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;
  switch(filetype) { //use switch (vb's select case) instead of if, because if == ugly
    case "byte": { //again, your using a string constant, not a variable, so it needs to be in quotes
      cout << "What do you want to change it into?"<<endl;
      cin >> change;
      switch(change) {
        case "kilobyte": {
          cout << "1024 bytes in a Kilobyte" << endl;
        } //end byte>>kilobyte case
        default: {
          cout << "Type not recongnized." << endl;
        } //end byte>?? case
      } //end switch for byte>>type
    } //end byte case
    default: {
      cout << "Type not recongnized." << endl;
    } // end ?? case
  } //end switch for type
  return 0xDEADBEEF; //ok, thats just leet..
}
Code: [Select]
joe@JoeMomma:~/dev/cpp/helloworld $ gcc Main.cpp
Main.cpp: In function `int main()':
Main.cpp:17: error: case label does not reduce to an integer constant
Main.cpp:20: error: switch quantity not an integer
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: If statments [C++]
« Reply #26 on: September 24, 2005, 05:20:18 pm »
Using a switch there is a pretty bad use for a switch statement. 

"else if" is want you want. 

This forum probably doesn't have the best people to ask about C, I've been noticing, so take whatever you learn here with a grain of salt. 

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: If statments [C++]
« Reply #27 on: September 25, 2005, 07:32:24 pm »
Lol, thanks Ron for the advise. :)  And thanks Joe, Sidoh, and Blaze for the help. :D
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 Eric

  • Full Member
  • ***
  • Posts: 304
  • I'm new here!
    • View Profile
Re: If statments [C++]
« Reply #28 on: September 25, 2005, 11:23:29 pm »
Switch can't make string-based comparisons.

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: If statments [C++]
« Reply #29 on: September 26, 2005, 12:01:27 am »
Switch can't make string-based comparisons.

You figured "not an integer" would clue him in. Guess not!
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT.