Thread: Basic c++ notepad problem.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Question Basic c++ notepad problem.

    So my problem is that I want users to be able to press enter without the program going to the next line, so the user can use another line of text.(Sorry I don't know a better way to explain it).
    Code:
    #include "resource.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(){
    	string text;
    	string name;
    	ofstream test;
    	string extension;
    	ifstream view;
    	char nv;
    
        cout<<"Do you want to view a file or make new one<n/v>?\n";
        cin>> nv;
        cin.ignore();
        if(nv == 'n'){
    	cout<<"Enter some text\n";
    	getline(cin, text);
    	cout<<"enter the file name and path(ex. c:/users/bob/filename)\n";
    	getline(cin, name);
    	cout<<"Enter the file extension(if you don't know one put '.txt'\n";
    	cin>> extension;
    	string file = name + extension;
    	test.open(file.c_str());
    	test<<text;
    	test.close();
        }
        if(nv == 'v'){
            string open;
        cout<<"enter the file name and path(ex. c:/users/bob/filename)\n";
    	getline(cin, name);
    	cout<<"Enter the file extension(if you don't know one put '.txt'\n";
    	cin>> extension;
    	string file = name + extension;
    	view.open(file.c_str());
    	while(getline(view,open))
        cout<<open<<endl;
    	view.close();
        }
    	system("pause");
    }

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    There is no standard way of doing it to my knowledge. The way I would do it would be to move the cursor (this is platform specific). Or maybe you can try adding backspaces (ie cout << '\b').
    Last edited by yaya; 02-12-2010 at 01:31 AM.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I haven't used c++ in a little while for a console app but you could always use string manipulation and implement your own way of adding new lines to 1 string.

    Just put a

    string totalString;
    Do while (char != someSortOfExitCharacter)

    getChar(whatever);
    and just append 'whatever' to the end of totalString;

    it should append newline characters and everything you add to the totalString.

    Then just save the totalString to a text file. When you view or print that text file, it should look the exact same way the user inputted each line. I did this a long time ago so I know it can be done. Just think and experiment.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Quote Originally Posted by stumon View Post

    string totalString;
    Do while (char != someSortOfExitCharacter)

    getChar(whatever);
    and just append 'whatever' to the end of totalString;
    I don't know what to use for the char in the do while loop.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Quote Originally Posted by bijan311 View Post
    I don't know what to use for the char in the do while loop.
    You can use a function like getChar(). This will get 1 character from the screen. You can check to see what that character is within a few if statements or a switch statement. Just add each character to the end of a string. You can even go as far as to check if the person hit the backspace key and then just delete the last character in the string. It takes some testing and looking up what the special characters integer representations are. Ex: Return, backspace, etc...
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Function Problem
    By AJOHNZ in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2009, 06:12 PM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. Basic compiling problem for a beginner
    By crazychile in forum C Programming
    Replies: 4
    Last Post: 09-21-2008, 02:27 AM
  4. Notepad Code Problem ???
    By keats in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 07:58 AM
  5. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM