Code not working.

Nothing pops up after I type in length. Any advice?(This code is a WIP)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;
char type1[26];
c



int main()
{
    cout << "Please choose length, tempature, or money contverters! Type exit to quit!" << endl;
    cin >> type1;
    if(type1=="length"){
        cout << "What measurements you like converted?";
    }
    

}
There is a syntax error caused by line 4.

On line 12 you are comparing the address of the array to the address of the c-style string literal - I can guarantee they will never be the same.

If you are going to use C++, use std::string and stop pretending you are using 'C with Classes'.
Last edited on
closed account (j3Rz8vqX)
What Lb meant:

type1[26] == "length--------------------";//possibly garbage; editor doesn't like spaces.

So, type1 == "length", would never be the same.

Therefore you would receive no print outs because it would never pass the if_condition test.

His advice was to redesign using std::string; string has a dynamic size, so if it should not have extended garbage.

Otherwise, your alternative is to compare characters inside the character array to see if a match was found - possibly the first 6 characters and ensure consecutive order.

Have fun.

Edited: Forget what I said. LB stands correct:
warning: comparison with string literal results in unspecified behaviour [-Waddress]
Last edited on
Dput wrote:
What Lb meant:

type1[26] == "length--------------------";//possibly garbage; editor doesn't like spaces.
I have absolutely no idea what you mean, but it is definitely not what I meant. When you compare an array and a string literal, you compare the addresses.
Topic archived. No new replies allowed.