Click here to Skip to main content
15,615,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

int main() {

    int myChoice;
    cout << "Do you want to (1 )read it or (2) write it ";
    cin >> myChoice;
    
    if (myChoice == 1)
    {
        string data;
        getline(cin,data);
        ofstream myFile;
        myFile.open("Afile.txt");
        if(myFile.is_open()) {

            myFile << data;
            myFile.close();

        } else {
            cout << "error";
    }

    } else if (myChoice == 2){
        ifstream myFile2;
        myFile2.open("Afile.txt");
        if(myFile2.is_open()){

            string x;
            getline(myFile2, x );
            cout << x ;
            myFile2.close();

        }
    }
    
}


What I have tried:

lllllllllllllllllllllll lllllllllllllllllllllll lllllllllllllllllllllll lllllllllllllllllllllll lllllllllllllllllllllll lllllllllllllllllllllll lllllllllllllllllllllll llllllllllllllllll
Posted
Updated 25-Feb-22 23:15pm
v3
Comments
Rick York 25-Feb-22 21:19pm    
Partly because you do not have logic to display output for every possible input.

A couple of things:
Your initial question is incorrect, option 1 is for writing, and 2 is for reading; so it should be:
C++
cout << "Do you want to (1 )write it or (2)read it ";

The reason it is not producing any output, is that the call to getline in the first method will read the line end character following the input option. You need to flush the input stream after the first call to cin:
C++
cin >> myChoice;
cin.ignore();

It would also be a good idea to output a message so the user knows what it is waiting for.
 
Share this answer
 
Quote:
Why this C++ code not showing any output?

There is not much outputs in this code. You can add some check point outputs (some outputs at strategic points in code to get a print every time execution cross such a point) to get an idea of what your code is doing, or turn to debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900