-4
#include <iostream>
#include <Windows.h>
using namespace std;

void noret()
{

    for (int i = 1; i < 11; i++)
    {
        cout << "Line number : " << i << endl;
    }

    system("pause");
}

void StartProgram(string filename)
{
    ShellExecute(NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL);
}

int main()
{
    for (int a = 1; a < 100; a += 3)
    {
        cout << "The number is: " << a << endl;
        if (a == 65)
        {

            StartProgram("mspaint");
        }
        else if (a != 65);
        {
            StartProgram("devenv");
        }
    }
    system("pause");
    return 0;

}

Here is the code I made(I am still new to programming). Please ignore the void noret() part. The code is Fully working, but in the part with else if (a != 65), I want to make it open the program only if it isn't equal to 65.

The program counts from 1-100. a = a+3 where "a" is equal to 1. While it counts to 100, if "a" is never equal to 65 it will open "devenv". But the way I did it it's made that "devenv" will open to very number that isn't equal to 65. How can I make it so that it will open ONCE if throughout the counting it never was 65... Does it make any sense?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
smartonion
  • 35
  • 4
  • 9
    Don't tell us to ignore things that aren't relevant to the question, instead leave out any irrelevant details. That function isn't called, and putting it in there on purpose, then drawing special attention to it, is really absurd. – tadman Dec 27 '17 at 19:36
  • 1
    The only way that branch of the `if` will fire is if `a` is not equal to `65`, so it's redundant to have the opposite condition in there. – tadman Dec 27 '17 at 19:36
  • So are you saying that I don't need the else if ? – smartonion Dec 27 '17 at 19:39
  • 2
    You don't need the if part that is after the else. – drescherjm Dec 27 '17 at 19:40
  • 2
    Also `if (a != 65);` with the semicolon there is a complete statement. So despite your indention the block under it will execute for any value of `a`. – drescherjm Dec 27 '17 at 19:42
  • Why?(I am sorry for not really understanding! like I said I am new to this) – smartonion Dec 27 '17 at 19:42
  • A debugger would help you see what is happening. I recommend using Visual Studio and single stepping through the code. – drescherjm Dec 27 '17 at 19:43
  • Related: https://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 – Barmar Dec 27 '17 at 19:50
  • "else if (a != 65);" - you did not really mean that final ";" there.. – Jesper Juhl Dec 27 '17 at 19:51
  • @smartonion Also relevaant: https://stackoverflow.com/questions/47959876/which-are-the-most-common-pitfalls-with-conditional-if-statements-in-c/47959897#47959897 – user0042 Dec 27 '17 at 20:05
  • So you want to start 98 devenv? 64 before mspaint and 34 after? – Killzone Kid Dec 27 '17 at 21:08

3 Answers3

4

This code is wrong many ways:

    if (a == 65)
    {
        StartProgram("mspaint");
    }
    else if (a != 65);
    {
        StartProgram("devenv");
    }

first of all semicolon after if makes it noop and terminates your else so that code is convoluted way to write:

    if (a == 65)
    {
        StartProgram("mspaint");
    }
    StartProgram("devenv");

just remove second if completely:

    if (a == 65)
    {
        StartProgram("mspaint");
    }
    else 
    {
        StartProgram("devenv");
    }

that to fix your code, to fix the logic of your program just use flag:

int main()
{
    bool found = false;
    for (int a = 1; a < 100; a += 3)
    {
        if (a == 65) found = true;
    }

    if( found ) 
        StartProgram("devenv");
    else
        StartProgram("mspaint");
}
Slava
  • 43,454
  • 1
  • 47
  • 90
2

If you want to know if all numbers in the loop are not 65, you need to remember whether you have seen 65 as you go through the loop:

auto found65 = false;
for (int a = 1; a < 100; a += 3)
{
    cout << "The number is: " << a << endl;
    found65 = found65 || (a == 65);
}

if (found65)
{

    StartProgram("mspaint");
}
else
{
    StartProgram("devenv");
}
NetMage
  • 26,163
  • 3
  • 34
  • 55
0

I assume that you have figured out the problems with your syntax, so I'll concentrate on high-level issues with the algorithm.

You don't need a loop to determine if counting by 3 would print 65. This can be done with simple math: when you start counting from a to z by x, you would hit n if (n-a) produces no remainder when divided by x:

bool see65 = (65-1) % 3 == 0;

This assumes that numbers a and z are on the opposite sides of n.

Since your conditional controls a single parameter, you can rewrite your call as a conditional expression:

StartProgram(see65  ? "mspaint" : "devenv");

Moreover, if you recall that bool in C++ is an integral type, you can eliminate conditional:

array<string,2> prog {"mspaint", "devenv"}
...
StartProgram(prog[see65]);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523