if else if problems

Nov 16, 2015 at 12:02am
I'm new to C++ and I'm not sure I understand how to use if else statements properly. I'm trying to make (for practice) a basic license verification program that checks the serial # against the length of the name input by the user. My problem is it never goes to the else statement when I run it. Here's what I have:

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
#include <iostream>
#include <string>
void v()
{
  std::cout << "Thank you for authorizing your product!";
}

int main()
{
  std::string fn;
  std::cout << "First Name: ";
  std::cin >> fn;
  int sn;
  std::cout << "Serial #: ";
  std::cin >> sn;
  int y = fn.length();
  if (y=1, y=3, y=7)
  {
    if (sn=12540)
    {
    v();
    }
  }
  else if (y=2, y=4, y=8)
  {
    if (sn=23651)
    {
    v();
    }
  }
  else if (y=5, y=9)
  {
    if (sn=34762)
    {
    v();
    }
  }
  else if (y=6, y=10)
  {
    if (sn=45873)
    {
    v();
    }
  }
else
  {
    std::cout << "Your information is not valid. Please check for typo's and try again.";
  }

  return 0;
}


I may be going about this entire solution the wrong way, but any help is much appreciated!
Nov 16, 2015 at 12:15am
in an if statement, you have to use two = instead of one.
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
#include <iostream>
#include <string>
void v()
{
  std::cout << "Thank you for authorizing your product!";
}

int main()
{
  std::string fn;
  std::cout << "First Name: ";
  std::cin >> fn;
  int sn;
  std::cout << "Serial #: ";
  std::cin >> sn;
  int y == fn.length();
  if (y==1, y==3, y==7)
  {
    if (sn==12540)
    {
    v();
    }
  }
  else if (y==2, y==4, y==8)
  {
    if (sn==23651)
    {
    v();
    }
  }
  else if (y==5, y==9)
  {
    if (sn==34762)
    {
    v();
    }
  }
  else if (y==6, y==10)
  {
    if (sn==45873)
    {
    v();
    }
  }
else
  {
    std::cout << "Your information is not valid. Please check for typo's and try again.";
  }

  return 0;
}
Last edited on Nov 16, 2015 at 12:16am
Nov 16, 2015 at 1:03am
Thanks! I was wondering about that, I tried it and now I'm having the opposite issue. It's ONLY going to the else statement, it seems there might be something wrong with the nested if statements? Also I'm getting these warnings when I compile:

||=== Build: Debug in auth (compiler: GNU GCC Compiler) ===|
||In function ‘int main()’:|
|17|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
|17|warning: value computed is not used [-Wunused-value]|
|18|warning: left operand of comma operator has no effect [-Wunused-value]|
|18|warning: right operand of comma operator has no effect [-Wunused-value]|
|25|warning: left operand of comma operator has no effect [-Wunused-value]|
|25|warning: right operand of comma operator has no effect [-Wunused-value]|
|32|warning: left operand of comma operator has no effect [-Wunused-value]|
|39|warning: left operand of comma operator has no effect [-Wunused-value]|
|17|warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
||=== Build finished: 0 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|

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
#include <iostream>
#include <string>
void v()
{
  std::cout << "Thank you for authorizing your product!";
}

int main()
{
  std::string fn;
  std::cout << "First Name: ";
  std::cin >> fn;
  int sn;
  std::cout << "Serial #: ";
  std::cin >> sn;
  int y;
  y==fn.length();
  if (y==1, y==3, y==7)
  {
    if (sn==12540)
    {
    v();
    }
  }
  else if (y==2, y==4, y==8)
  {
    if (sn==23651)
    {
    v();
    }
  }
  else if (y==5, y==9)
  {
    if (sn==34762)
    {
    v();
    }
  }
  else if (y==6, y==10)
  {
    if (sn==45873)
    {
    v();
    }
  }
else
  {
    std::cout << "Your information is not valid. Please check for typo's and try again.";
  }

  return 0;
}



Nov 16, 2015 at 1:09am
in if statements, you cannot use commas. for what you are trying to do use && in place of them.
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
#include <iostream>
#include <string>
void v()
{
  std::cout << "Thank you for authorizing your product!";
}

int main()
{
  std::string fn;
  std::cout << "First Name: ";
  std::cin >> fn;
  int sn;
  std::cout << "Serial #: ";
  std::cin >> sn;
  int y = fn.length();
  if (y==1 && y==3 && y==7)
  {
    if (sn==12540)
    {
    v();
    }
  }
  else if ((y==2 && y==4) && y==8)
  {
    if (sn==23651)
    {
    v();
    }
  }
  else if (y==5 && y==9)
  {
    if (sn==34762)
    {
    v();
    }
  }
  else if (y==6 && y==10)
  {
    if (sn==45873)
    {
    v();
    }
  }
else
  {
    std::cout << "Your information is not valid. Please check for typo's and try again.";
  }

  return 0;
}
Nov 16, 2015 at 1:51am
Good to know, I spent a good while reading around the internet trying to figure that out. I replaced the commas and that got rid of the warnings from the compiler but the program is still going to the else statement after it asks for the serial #. Any thoughts?
Nov 16, 2015 at 2:42am
the first if statement was missing a pair of parentheses.
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
#include <iostream>
#include <string>
using namespace std;
void v()
{
  cout << "Thank you for authorizing your product!";
}

int main()
{
  string fn;
  cout << "First Name: ";
  cin >> fn;
  int sn;
  cout << "Serial #: ";
  cin >> sn;
  int y = fn.length();
///  cout<<endl<<y<<endl;
  if ((y==1 && y==3) && y==7)
  {
    if (sn==12540)
    {
    v();
    }
  }
  else if ((y==2 && y==4) && y==8)
  {
    if (sn==23651)
    {
    v();
    }
  }
  else if (y==5 && y==9)
  {
    if (sn==34762)
    {
    v();
    }
  }
  else if (y==6 && y==10)
  {
    if (sn==45873)
    {
    v();
    }
  }
else
  {
    cout << "Your information is not valid. Please check for typo's and try again.";
  }

  return 0;
}
Nov 16, 2015 at 3:58am
I tried that and I was still getting the same results. I made the else statement into another function and added else statements for all the nested if statements. Works like a charm now! Thanks for your help

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
#include <iostream>
#include <string>
void v()
{
  std::cout << "Thank you for authorizing your product!";
}

void nv()
{
  std::cout << "Your information is not valid. Please check for typo's and try again.";
}

int main()
{
  std::string fn;
  std::cout << "First Name: ";
  std::cin >> fn;
  int sn;
  std::cout << "Serial #: ";
  std::cin >> sn;
  int y;
  y=fn.length();
  if ((y==1 or y==3) or y==7)
  {
    if (sn==12540)
    {
    v();
    }
    else {nv();}
  }
  else if ((y==2 or y==4) or y==8)
  {
    if (sn==23651)
    {
    v();
    }
    else {nv();}
  }
  else if (y==5 or y==9)
  {
    if (sn==34762)
    {
    v();
    }
    else {nv();}
  }
  else if (y==6 or y==10)
  {
    if (sn==45873)
    {
    v();
    }
    else {nv();}
  }
else
  {
    nv();
  }

  return 0;
}


Topic archived. No new replies allowed.