C++ Loop not working user input

Some of my loop is not working. How messed up is this code... Please someone help. Commented non working 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
// ADIT.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "windows.h"

#include <iostream>
#include <istream>
#include <fstream>
#include <string>

using namespace std;

string FileName;
string Contents;
string szCompletion;
string szClass, dcClass;
string szAnswer;
string First, Last, Ranker;

ifstream infile;
ofstream outfile;

int _tmain(int argc, _TCHAR* argv[])
{
 SetConsoleTitle(L"FILER");

 cout << "THIS IS A FILER\n\n" ;
 cout << "Automatically FILL after completion?[Y/N] ";
 getline(cin, szCompletion);
 cout << "MILE or SMILE? ";
 getline(cin, szClass);

 if(szClass == "MILE"){
  dcClass = ",FILER";
 }
 else{
  dcClass = ",FILER";
 }


 bool Correct = false;

 do{
  cout << "Enter the file name:  ";
        getline(cin, FileName);
  cout << "\n";
        infile.open (FileName, ifstream::in|ios::out);

  if (infile.good()){
   while ( !infile.eof() ){
    getline(infile, Contents);
    cout << Contents << endl; // Prints our STRING.
   }

   cout << "\nIs this correct?[Y/N] ";
   getline(cin, szAnswer);
   infile.close();

   if (szAnswer == "Y" || "y" || "YES" || "Yes"){//If anything other then these I need it to ask for filename again. :(
    cout << "\nReading file......\n\n" ;
    Sleep(2000);
    Correct = true;
   }
  }
  else
   cout << "File not found!!\n";
 }
 while(!Correct);

 infile.open (FileName, ifstream::in|ios::out);
 outfile.open ("Tester.txt");

 if (infile.good()){
  while (!infile.eof()){
    cout << "good";
   }
 }

 infile.close();
 outfile.close();
 if(szCompletion == "Y" || "y" || "YES" || "Yes"){//If they selected N in the begining skip this. Not working :(
	int i;
	cout << "Attempting to access Command.com\n";
	if (system(NULL)) puts ("Ok\n");
    else exit (EXIT_FAILURE);
	cout << "Executing command...\n";
	i=system("ipconfig /all");//
	cout << "The value returned was: %d.\n" << i;
 }

 system("Pause");
 return 0;
}
This isn't how you check multiple conditions in C++.
if (szAnswer == "Y" || "y" || "YES" || "Yes")

You need to do something like this.
if (szAnswer == "Y" || szAnswer == "y" || szAnswer == "YES" || szAnswer == "Yes")
Last edited on
cout << "The value returned was: %d.\n" << i; <.< I didn't know you could do this in C++. I thought that was only for C.

Umm, this if(szCompletion == "Y" || "y" || "YES" || "Yes") doesn't work for some reason. I have tried doing it like this before but I never succeed.\

This is the way I do it:

if((szCompletion == "Y") || (szCompletion == "y")|| (szCompletion == "YES") || (szCompletion == "Yes"))

Long, I know but it does the trick. It has to do with the booleaness of if statements. The way you were doing it was saying...if szCompletion == "Y" or y or YES or Yes. Only the first boolean was checked but the rest were not really checking anything because there was no assignment to them. You have to explicitly tell it what to do.
Thank you both very much
Topic archived. No new replies allowed.