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.

Sidoh

Quote from: iago on September 24, 2005, 11:55:32 AM
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. ^^

AntiVirus

When I put  } elseif(change == "kilobyte") {

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

Here is my code again:
#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

Blaze

There isn't an elseif in C++, that I know of..
And like a fool I believed myself, and thought I was somebody else...

AntiVirus

#18
That's what I thought, but Joe seems to think there is.
Quote from: Joe[e2] 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)

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;
}
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

Quote from: Brandon on September 24, 2005, 01:15:08 PM
That's what I thought, but Joe seems to think there is.

Joe is a VB nub (jk joe)

Blaze

And like a fool I believed myself, and thought I was somebody else...

Sidoh

Quote from: Blaze on September 24, 2005, 01:27:55 PM
Try using Select instead.
That wouldn't be logical in this situation.  Besides, C doesn't have Select, it has switch.  :)

Blaze

And like a fool I believed myself, and thought I was somebody else...

AntiVirus

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

Sidoh

Quote from: Blaze on September 24, 2005, 01:31:19 PM
Ugh... shh!
lol

Quote from: Brandon on September 24, 2005, 01:32:53 PM
There is a "else if (blah = blah) thing. Just elseif can't be together.
That's not really a special operator, though.  doing:

if(0) { ... } else if(1) { ... } is the same thing as:


if(0) {
   ...
} else {
   if(1) {
      ...
   }
}

Joe

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.
#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..
}

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
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.


iago

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. 

AntiVirus

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

Eric

Switch can't make string-based comparisons.

Newby

Quote from: LoRd[nK] on September 25, 2005, 11:23:29 PM
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

Quote from: Rule on June 30, 2008, 01:13:20 PM
Quote from: CrAz3D on June 30, 2008, 10:38:22 AM
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.