• Log in or sign up for an account here: https://www.sythe.org/login

Can anyone overlook my C++ program and correct me please? (VERY NOOB!!)

mstandlee

Member
Joined
May 3, 2007
Sythe Gold
0
Messages
65
Ok. So I got interested in writing C++ after I saw a thread here. I have wanted to know how to program for a while, jus never taken the time to learn it. My programming is still VERY basic, but I would like someone to overlook it and tell me what Im doing wrong. I want it to first not end the program until you press enter. Also, It writes a new text document wether the answer is true or false. Please help me out..

Code:
#include <iostream>
#include <stdio.h> //needed for the fopen command. Writes the txt file

using namespace std;

int main()

{
    
    int yesorno;

  cout<<"Would you like to create the file NEWTXTFILE?: "; //this is the test line
  cin>> yesorno;
  cin.ignore(); 
  if (yesorno = true)
  {
   fopen ("newtxtfile.txt", "w+");
   cout<<"The file has been written\n"; //successfully writes a new text file
   cout<<"This program will now close... Press enter to terminate Program.\n";
   cin.get();
      }
  else if (yesorno = false)
 {
  cout<<"The file has not been written and this program will now close\n";
  cout<<"Press enter to terminate Program.\n";
  cin.get();
      }
}
Thanks to anyone who wouldnt mind taking a look.

mstandlee
 
For one, you should use the boolean operator == in the if () statement

ie you're saying if i set yesorno equal to true and it's value is >= 1, then do this, else do this; assuming true holds a value of >= 1, this will always return true, and there can be no chance of the file not being written.

so if (yesorno == true) (which is really the same as if (yesorno))
and else if (yesorno == false) (again, the same as if (!yesorno))

just a start
 
duelarena.gg
Back
Top