Hey Everone ,

I Wrote a Program To Enter The info and the given info would be saved to a text file .

Here is The Program Itself :

/* Created By Hayzam Sherif 
   Date : 11/7/11'
*/



#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{
    char name[100];
    int age;
    char choice[100];
    
    
    cout<<"Name :";
    cin>>name;
    cout<<"Age :";
    cin>>age;
    cout<<name<<"\t"<<age<<" : Should I Save This ?"<<endl;
    if(choice == 'y');
    {
              
              cout<<"OK"<<endl;
              ofstream info;
              info.open ("info.txt");
              info<<name<<age<<endl;
              info.close();
                           }
                           
     else(choice == 'n');
     {
               break;
                 }
     
     else if(choice != 'y' || 'n');
     {
          cout<<"That's Not an option"<<endl;
          break;
          }
    
    
    system("Pause");
    return 0;
    }

I Cannot Understand The Errors xD

Sorry For My Bad English.

Dani AI

Generated

Quick summary tied to the existing replies: the program’s goal (ask name and age, then save on a Y/N confirmation) is fine, but several small mistakes cause compile-time or runtime problems. ’s rewrite is on the right track and correctly flagged the stray semicolons, but a few other issues and best practices are worth clarifying.

Core problems and why they fail

  • Comparing an array or pointer to a character (e.g. if (choice == 'y')) is not doing what was intended; that compares addresses, not the stored character. Read a single char (not a char[]) for the choice or use std::string and inspect its first character.
  • Trailing semicolons after an if/else make the conditional apply to an empty statement; the following block will always execute.
  • break outside of a loop or switch is a compile error. Use return/exit or restructure control flow.
  • if (choice != 'y' || 'n') is logically wrong; || 'n' doesn’t compare anything. Use if (choice != 'y' && choice != 'n') or a safe else branch.
  • cin >> name stops at whitespace; use std::string plus std::getline when full names (with spaces) are required, and remember to consume the leftover newline after numeric input.

Practical tips and small enhancements

  • Prefer std::string name; and std::getline for names. After cin >> age; call cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); before a subsequent getline.
  • Read confirmation as char choice; std::cin >> choice; choice = std::tolower(choice); (include <cctype>). Then test if (choice == 'y'), else if (choice == 'n'), else for invalid input.
  • Open files with error checking and choose append vs overwrite explicitly: std::ofstream info("info.txt", std::ios::app); if (!info) { std::cerr << "Cannot open file\n"; return 1; }. Rely on RAII instead of calling info.close() manually unless needed.
  • Validate numeric input (if (!(cin >> age)) { /* handle bad input */ }) and avoid system("Pause") — use portable input pauses if absolutely necessary.

Checklist before recompiling

  • Remove stray semicolons after conditionals.
  • Replace break with proper control flow.
  • Initialize and actually read the choice variable.
  • Add <string>, <cctype> and <limits> where used.

These fixes address the compile errors and make the program more robust and user-friendly while keeping the logic straightforward.

Recommended Answers

All 3 Replies

#include <fstream>
#include <iostream>
#include <cstdlib>
 
using namespace std;
int main()
{
    char name[100];
    int age;
    char choice[100];
 
 
    cout<<"Name :";
    cin>>name;
    cout<<"Age :";
    cin>>age;
    cout<<name<<"\t"<<age<<" : Should I Save This ? \n (Y/N)"<<endl;
	cin>>choice[0];
    if(choice[0] == 'y')
    {
 
              cout<<"OK"<<endl;
              ofstream info;
              info.open ("info.txt");
              info<<name<<age<<endl;
              info.close();
                           }
 
     else if(choice[0] == 'n')
     {
               exit(0);
                 }
 
     else if(choice[0] != 'y' || 'n')
     {
          cout<<"That's Not an option"<<endl;
          exit(0);
          }
 
 
    system("Pause");
    return 0;
    }

without knowing what the errors are (I'm not going to try it myself), here's some things that are immediately clear:
- you're missing several #include directives, starting with iomanip and istream.
- you're comparing the array choice with the character 'y' (this might implicitly work, but is very nasty programming style).
- you have semicolons terminating your if and else statements. This is almost always incorrect.

Please Just Give Me a Clean Code Will You? Cuz' I Tried My Best on This Program.BTW No Offence Here :D.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.