If(...)#include

How do I do this:
1
2
3
4
5
6
7
if(Operating System = Windows){
#include <cstdlib.h>
}
else{
"Do not include <cstdlib.h>"
}
//code goes here 
Last edited on
1
2
3
4
5
#ifdef _WIN32
  #include <cstdlib>
#else
  // something else?
#endif 


[edit]
BTW, it looks like you are doing something unkosher. You normally see something like this:

1
2
3
4
5
#ifdef _WIN32
  #include <windows.h>
#else
  #include <unistd.h>
#endif 
Last edited on
And I can use system commands easily? The executable will start? What I mean is, the program starts regardless of OS, but if a Non-Windows OS user uses one of the system commands, they will get an error message in the program, not before the executable starts.
So if I use the following code:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
#include <string>
#include <ctype.h>
#ifdef _WIN32
#include <cstdlib>
#else
#include <unistd.h>
#endif
using namespace std;
double Ascendn1, Ascendam, Ascendjump, Descendn1, DescendAm, Descendjump, VariableNumber;
string repeat, VariableString, VariableStringrn, Deleter, Helpp;
int repeatnum = 0, VariableChoose;
string prompt = "|-[]->";
int Calculator();
int main()
{
    system("COLOR 0a");
    cout << "Enter an input";
    string entry;
    bool Running = true;
    while(Running == true)
    {
    cout << "\n";
    cout << prompt;
    cin >> entry;
    if(entry == "Destroy" || entry == "destroy")
    {
        VariableString = "";
        VariableNumber = 0;
    }
    if(entry == "Exit") return 0;
    if(entry == "Help" || entry == "help")
    {
        cin >> Helpp;
        if(Helpp == "Command" || Helpp == "command")
        {
            cout << "\nDisplay a list of the Most Used Commands";
        }
        else if(Helpp == "Recall" || Helpp == "recall")
        {
            cout << "\nDisplay the Variables and their Values";
        }
        else if(Helpp == "Calculator" || Helpp == "calculator")
        {
            cout << "\nStart up the simple calculator";
        }
        else if(Helpp == "Print" || Helpp == "print")
        {
            cout << "\nPrint out the specified string";
        }
        else if(Helpp == "/")
        {
            cout << "Clear the Variables Values";
        }
        else if(Helpp == "VariableNumber")
        {
            cout << "\nStores a number into a variable";
        }
        else if(Helpp == "VariableString")
        {
            cout << "\nStores a string into a variable";
        }
        else if(Helpp == "Ascend" || Helpp == "ascend")
        {
            cout << "\nCount up from a number";
        }
        else if(Helpp == "Descend" || Helpp == "descend")
        {
            cout << "\nCounts down from a number";
        }
        else if(Helpp == "ChangePrompt" || Helpp == "changeprompt")
        {
            cout << "\nChange the input prompt";
        }
        else if(Helpp == "Repeat" || Helpp == "repeat")
        {
            cout << "\nRepeat a string multiple times";
        }
        else if(Helpp == "" || Helpp == " ")
        {
            cout << "Write 'Help' and then a keyword to get help on it.\nExample: 'Help Repeat'";

        }
    }
    if(entry == "Command")
    {
        cout << "List of Commands:\n(Exit) End the Program\n(Help) Get help\n(Command) Display a list of commands.\n(Ascend) Counts up from a number\n(Descend) Counts down from a number\n(ChangePrompt) Change the input prompt\n(Repeat) Repeat a string for a certain amount of times.\n(Calculator) Start a simple Calculator";
    }
    if(entry == "Recall" || entry == "recall")
    {
        cout << "VariableNumber = " << VariableNumber;
        cout << endl;
        cout << "VariableString = " << VariableString;
    }
    if(entry == "Calculator" || entry == "calculator")
    {
        Calculator();
    }
    if(entry == "print" || entry == "Print")
    {
        getline(cin, repeat);
        if(repeat == " VariableString") repeat = VariableString;
        cout << repeat;
    }
    if(entry == "/")
    {
        getline(cin, Deleter);
        if(Deleter == "VariableNumber")
        {
            VariableNumber = 0;
        }
        else if(Deleter == "VariableString")
        {
            VariableString == "";
        }
    }
    if(entry == "VariableNumber")
    {
        cin >> VariableNumber;
    }
    if(entry == "VariableString")
    {
        getline(cin, VariableString);
    }
    if(entry == "Ascend" || entry == "ascend")
    {
        cout << "Input your number: ";
        cin >> Ascendn1;
        cout << "How many times do you want to ascend?: ";
        cin >> Ascendam;
        cout << "How much do you want to jump forward each time?: ";
        cin >> Ascendjump;
        for(Ascendam; Ascendam > 0; Ascendam--)
        {
            Ascendn1 += Ascendjump;
            cout << Ascendn1 << "\n";
        }
    }
    if(entry == "Descend" || entry == "descend")
    {
        cout << "Enter your starting number: ";
        cin >> Descendn1;
        cout << "How much do you want to descend?: ";
        cin >> DescendAm;
        cout << "How much do you want to jump back?: ";
        cin >> Descendjump;
        for(DescendAm; DescendAm > 0; DescendAm--)
        {
            Descendn1 -= Descendjump;
            cout << Descendn1 << "\n";
        }
    }
    if(entry == "ChangePrompt" || entry == "changeprompt")
    {
        cout << "What do you want to change the prompt to?: ";
        cin >> prompt;
        if(prompt == "VariableString")
        {
            prompt = VariableString;
        }
    }
    if(entry == "Repeat" || entry == "repeat")
    {
        cout << "What string do you want to repeat?: ";
        getline(cin, repeat);
        if(repeat == "VariableString") repeat = VariableString;
        if(repeat == "VariableNumber") repeat = VariableNumber;
        cout << "How many times do you want to repeat " << repeat << "?: ";
        cin >> repeatnum;
        if(repeatnum > 0)
        {
        for(repeatnum; repeatnum > 0; repeatnum--){cout << repeat << "\n";}
        }
    }
    if(entry == "Store" || entry == "store")
    {
        cout << "What type of variabel do you want to use?\n1) Number\n2) String: ";
        cin >> VariableChoose;
        if(VariableChoose == 1)
        {
            cout << "What value do you want to store?: ";
            cin >> VariableNumber;
            cout << VariableNumber << " succesfully stored.";
        }
        if(VariableChoose == 2)
        {
            cout << "What value do you want to store?: ";
            getline(cin, VariableString);
            cout << VariableString << " succesfully stored.";
        }
        cout << "\nTo Recall the Variable that you stored, input 'Recall' at the command prompt, and then when asked, input either 'VariableString' or 'VariableNumber', depending on the variable type.";
    }
    }
    char f;
    cin >> f;
    return 0;
}
int Calculator()
{
    double n1, n2;
    char sel;
    cout << "Welcome to the Calculator, you can add, subtract, multiply and divide two numbers";
    cout << "Enter your first Number: ";
    cin >> n1;
    cout << "Enter your second Number: ";
    cin >> n2;
    cout << "What do you want to do with the numbers " << n1 << " and " << n2 << "?\nA dd\nS ubtract\nM ultiply\nD ivide";
    cin >> sel;
    if(sel == 'A' || sel == 'a')
    {
        cout << n1 << " + " << n2 << " = " << n1 + n2;
    }
    if(sel == 'S' || sel == 's')
    {
        cout << n1 << " - " << n2 << " = " << n1 - n2;
    }
    if(sel == 'M' || sel == 'm')
    {
        cout << n1 << " * " << n2 << " = " << n1 * n2;
    }
    if(sel == 'D' || sel == 'd')
    {
        cout << n1 << " / " << n2 << " = " << n1 / n2;
    }
    return 0;
}

on a linux operating system, the executable will still start, and the program run?
Last edited on
Possibly, but I'm not sure that linux has the color batch file. Anywhere you see the system(...) call, you would need to check to make sure that linux has something in it's console window. A good rule of thumb though is if it works in windows, more than likely it doesn't work in linux.
Thanks for the Help:)
A better alternative would be to change:
system("COLOR 0a");
To:
1
2
3
#ifdef _WIN32
system("COLOR 0a");
#endif 
Topic archived. No new replies allowed.