Please please help!!!!!!!!!!

I am new to C++ and I have the following code, and my if and else if statements are not working when I run it. It compiles fine.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
using namespace std;
#define PI 3.14159
int main()
{
    char structure[100];
    cout << "Enter your type of structure:";
    cin >> structure;

    if ((structure == "I-beam") || (structure == "I-Beam") || (structure == "ibeam") || (structure == "i-beam")){
    string mystr;
    float b1=0;
    float b2=0;
    float b3=0;
    float d1=0;
    float d2=0;
    float d3=0;
    float c2=0;
    float I1=0;
    float I2=0;
    float I3=0;
    cout << "Enter b1:";
    getline (cin,mystr);
    stringstream(mystr)>> b1;
    cout << "Enter b2:";
    getline (cin,mystr);
    stringstream(mystr)>> b2;
    cout << "Enter b3:";
    getline (cin,mystr);
    stringstream(mystr)>> b3;
    cout << "Enter d1:";
    getline (cin,mystr);
    stringstream(mystr)>> d1;
    cout << "Enter d2:";
    getline (cin,mystr);
    stringstream(mystr)>> d2;
    cout << "Enter d3:";
    getline (cin,mystr);
    stringstream(mystr)>> d3;
    cout << "c2:" << (((b1 * d1) * (d1 / 2)) + ((b2 * d2) * (d1 + (d2/2))) + ((b3 * d3) * (d1 + d2 + (d3/2)))) / ((b1 * d1) + (b2 * d2) + (b3 * d3)) << endl;
    cout << "I1:" << ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) << endl;
    cout << "I2:" << ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)) << endl;
    cout << "I3:" << ((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2)) << endl;
    cout << "The second Moment of I:" <<(((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)))+(((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2))) + (((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2))) << endl;
}
    else if ((structure == "c-section") || (structure == "C-Section") || (structure == "C-section") || (structure == "c-Section"))
    {
    string napti;
    float d1=0;
    float d2=0;
    cout << "Enter d1:";
    getline (cin,napti);
    stringstream(napti)>> d1;
    cout << "Enter d2:";
    getline (cin,napti);
    stringstream(napti)>> d2;
    cout << "I: " <<(PI / 64 * (d2 * d2 * d2 * d2 - d1 * d1 * d1 *d1))<< endl;
    }
    system ("PAUSE");
    return 0;
}

Last edited on
How are they not working? What is happening?
warning: comparison with string literal results in unspecified behaviour
In that case you should use single quotes instead of double quotes.
No warnings or anything like that when I run the program and it asks for what type of structure I type in unwanted but then it says to press any key to continue so I do and it exits the program
Not unwanted I meant ibeam
When you write structure == "I-beam" you compare two pointers, not two strings. It works as any other pointer comparison - it just compares two addresses, which is not your intention.

To compare two c-style strings (char*) you should use the strcmp function: if (strcmp(structure, "I-beam") == 0).... If you don't care about the upper/lowercase - use stricmp instead.

The other approach is to use std::string instead of char*. It has an overloaded operator==, so
1
2
3
std::string structure;
// ...
if (structure == "I-beam")

would do what you expect - compare two strings.

The choice is yours (I would rewrite it with std::string).
now im getting this error
error: cannot convert `bool' to `const char*' for argument `1' to `int strcmp(const char*, const char*) for arguement '1' to int strcmp(constant char...

that error was when i used the strcmp functon, i tried the other one too, but the error then told me that structure was not part of std
Last edited on
Looks like you use the function incorrectly: if (strcmp(structure == "I-beam")) instead of if (strcmp(structure, "I-beam") == 0). Am I right? If not - then post the code so I could see what's wrong.

Last edited on
Also this looks weird:
1
2
getline (cin,mystr);
stringstream(mystr)>> b1;


Is there a specific reason for not doing it like this? -
 
cin >> b1;
Thats what I ended up doing is the

cin >> b1;
Topic archived. No new replies allowed.