C++ question

Discussion in 'Programming' started by Imozeb, Dec 27, 2011.

  1. #1
    Why do I get an infinite loop for the switch? And how do I save to a sub folder like: \saves\my_file.txt


    CODE:
    // Test-3.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    //functions
    int write_file()
    {
    string text;
    string file_name;
    string ffile_name;

    cout<<"Please write your text: ";
    getline(cin,text);
    getline(cin,text);
    cout<<"\nEnd Text\n\nThank you.\n\nPlease enter your file name: ";
    getline(cin,file_name);
    ffile_name.append(file_name);
    ffile_name.append(".txt");
    cout<<"\nEnd File Name\n\nThank you.\n\nWriting "<<text<<" to "<<ffile_name<<"";

    ofstream myfile;
    myfile.open (ffile_name);
    myfile << text;
    myfile.close();
    cout<<"\n\nEnd";
    return 0;
    }

    int read_file()
    {
    string file_name;
    string text;

    cout<<"\nPlease enter the file name you wish to read: ";
    getline(cin,file_name);
    getline(cin,file_name);
    file_name.append(".txt");

    ifstream myfile (file_name);
    if (myfile.is_open())
    {
    cout<<"\n\nData in "<<file_name<<":\n";
    while ( myfile.good() )
    {
    getline (myfile,text);
    cout << text << endl;
    }
    myfile.close();
    }
    cout<<"\n\nEnd";
    return 0;
    }

    //int vars
    int choice;

    //main
    int main()
    {
    while(0==0)
    {
    cout<<"\nEnter the number of your choice from the list below.";
    cout<<"\n1. Read File";
    cout<<"\n2. Write File";
    cout<<"\n";
    cin>>choice;
    if(isdigit(choice)==0)
    {
    switch(choice)
    {
    case 1:
    read_file();
    break;
    case 2:
    write_file();
    break;
    }
    }
    else
    {
    cout<<"\nError";
    }
    }
    }


    Thank you.
     
    Imozeb, Dec 27, 2011 IP
  2. visualgaurd

    visualgaurd Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    by while function you will get infinite loop and for up site you declare file extension so your file would be save when program will execute.
     
    visualgaurd, Dec 27, 2011 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What do you mean for up site declare function? COuld you give me an example?
     
    Imozeb, Dec 28, 2011 IP
  4. sanjogm

    sanjogm Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    For learning C++, what software should I install in my computer?
     
    sanjogm, Dec 29, 2011 IP
  5. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #5
    c++ .
     
    drhowarddrfine, Dec 29, 2011 IP
  6. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #6
    Please use a better translator that outputs standard English. What you have said here looks like monkeys dancing on your keyboard.
     
    mmerlinn, Dec 30, 2011 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I know. Could someone explain it to me in proper english. Thank you.
     
    Imozeb, Dec 30, 2011 IP
  8. mco65

    mco65 Member

    Messages:
    144
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    38
    #8
    Code::Blocks and MINGW
     
    mco65, Jan 6, 2012 IP