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

0 Members and 1 Guest are viewing this topic.

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.