samedi 9 janvier 2016

Trying to figure out why when writing to file, the last character written is being duplicated when cout looks fine on display

Vote count: 0

Question = Trying to figure out why the write to file for

 myfile.open ("Crypt103Data.txt", ios::out | ios::app);
 myfile <<tab2[code1];
 myfile.close();

is writing the last character twice to Crypt103Data.txt when the cout which also runs for the same iterations within the while loop displays proper without the extra character write appended to file?

If you enter something like Hello_World you might see something like r$cc7fq7UcX depending on what seed value to entered which gives the output a unique output that is dependent on the seed key. While r$cc7fq7UcX is observed correctly on the screen below the Hello_World , what is written to file is r$cc7fqUcXX where the last character is written twice and it should only be r$cc7fqUcX . Not sure why this happens when the cout print to display and file write appended happens for the same iterations within the while loop. Instead of placing a band aid in to trim the last character from the file after each line is written, there must be a fix for this odd bug that someone here might be able to point out. Maybe I am using sloppy programming that is causing for this to occur. I am using the Bloodshed Dev c++ 4.9.9.2 IDE btw

#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
// 12/30/2015 - Added string random shuffle
// 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key
// 1/4/2016 - Added ability to enter string and have while loop process v1.00
// 1/5/2016 - Added characters , and ; to supported characters which were missing from v1.01
// 1/8/2015 - Source code trash cleanup & offset band aid removed for IF statement logic +1 array offset v1.02
// 1/8/2015 - Added write to file for coded data storage eliminating need to copy/paste output to a text file v1.03
using namespace std;

int main(int argc, char *argv[])
{

    int seed1=0;
    int code1=0;
    int run=1;
    int again=1;
    int test=1;
    char ch1;
    char ch2;


        while(again==1){



    cout<<" Crypt Version 1.03\n\n";
    cout<<"XXXXXXXXXXXXXXXXXXXXX\n";
    cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed
    cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n";
    cin >> seed1; // Input user seed
    cout<<"\n\n"<<"Crypt String Key =\n\n";

              ofstream myfile;
  myfile.open ("Crypt103Key.txt", ios::out | ios::app);
  myfile <<seed1<<"\n";
  myfile.close();






    //Initialize Valid Characters for String Shuffle Output
    //Note Bug corrected with blank space for \ by use of escape character proceeding
    string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;";



    srand(seed1); // Allows user custom seeded starting algorithm position for random

    random_shuffle(str.begin(), str.end()); // Shuffle the string
    cout << str << "\n\n\n"; // Output the shuffle sequence


        //Pass String Output into an array to pair up pointer value with associated character

    string tmp = str; //Pass str output to string tmp
    char tab2[128]; // Memory Allocation for array population
    strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array
    tab2[sizeof(tab2) - 1] = 0;


        cout<<"Enter Info to Crypt in correct case\n";
        cout<<"To Exit Inner Program enter  ( ` ) \n\n\n";

    while(run==1){

    cin>>ch1;

    if (ch1=='A'){
                code1=0;
                }
    else if (ch1=='B'){
         code1=1;
         }
    else if (ch1=='C'){
         code1=2;
         }
    else if (ch1=='D'){
         code1=3;
         }
    else if (ch1=='E'){
         code1=4;
         }
    else if (ch1=='F'){
         code1=5;
         }
    else if (ch1=='G'){
         code1=6;
         }
    else if (ch1=='H'){
         code1=7;
         }
    else if (ch1=='I'){
         code1=8;
         }
    else if (ch1=='J'){
         code1=9;
         }
    else if (ch1=='K'){
         code1=10;
         }
    else if (ch1=='L'){
         code1=11;
         }
    else if (ch1=='M'){
         code1=12;
         }
    else if (ch1=='N'){
         code1=13;
         }
    else if (ch1=='O'){
         code1=14;
         }
    else if (ch1=='P'){
         code1=15;
         }
    else if (ch1=='Q'){
         code1=16;
         }
    else if (ch1=='R'){
         code1=17;
         }
    else if (ch1=='S'){
         code1=18;
         }
    else if (ch1=='T'){
         code1=19;
         }
    else if (ch1=='U'){
         code1=20;
         }
    else if (ch1=='V'){
         code1=21;
         }
    else if (ch1=='W'){
         code1=22;
         }
    else if (ch1=='X'){
         code1=23;
         }
    else if (ch1=='Y'){
         code1=24;
         }
    else if (ch1=='Z'){
         code1=25;
         }
    else if (ch1=='a'){
         code1=26;
         }
    else if (ch1=='b'){
         code1=27;
         }
    else if (ch1=='c'){
         code1=28;
         }
    else if (ch1=='d'){
         code1=29;
         }
    else if (ch1=='e'){
         code1=30;
         }
    else if (ch1=='f'){
         code1=31;
         }
    else if (ch1=='g'){
         code1=32;
         }
    else if (ch1=='h'){
         code1=33;
         }
    else if (ch1=='i'){
         code1=34;
         }
    else if (ch1=='j'){
         code1=35;
         }
    else if (ch1=='k'){
         code1=36;
         }
    else if (ch1=='l'){
         code1=37;
         }
    else if (ch1=='m'){
         code1=38;
         }
    else if (ch1=='n'){
         code1=39;
         }
    else if (ch1=='o'){
         code1=40;
         }
    else if (ch1=='p'){
         code1=41;
         }
    else if (ch1=='q'){
         code1=42;
         }
    else if (ch1=='r'){
         code1=43;
         }
    else if (ch1=='s'){
         code1=44;
         }
    else if (ch1=='t'){
         code1=45;
         }
    else if (ch1=='u'){
         code1=46;
         }
    else if (ch1=='v'){
         code1=47;
         }
    else if (ch1=='w'){
         code1=48;
         }
    else if (ch1=='x'){
         code1=49;
         }
    else if (ch1=='y'){
         code1=50;
         }
    else if (ch1=='z'){
         code1=51;
         }
    else if (ch1=='1'){
         code1=52;
         }
    else if (ch1=='2'){
         code1=53;
         }
    else if (ch1=='3'){
         code1=54;
         }
    else if (ch1=='4'){
         code1=55;
         }
    else if (ch1=='5'){
         code1=56;
         }
    else if (ch1=='6'){
         code1=57;
         }
    else if (ch1=='7'){
         code1=58;
         }
    else if (ch1=='8'){
         code1=59;
         }
    else if (ch1=='9'){
         code1=60;
         }
    else if (ch1=='0'){
         code1=61;
         }
    else if (ch1=='!'){
         code1=62;
         }
    else if (ch1=='@'){
         code1=63;
         }
    else if (ch1=='#'){
         code1=64;
         }
    else if (ch1=='$'){
         code1=65;
         }
    else if (ch1=='%'){
         code1=66;
         }
    else if (ch1=='^'){
         code1=67;
         }
    else if (ch1=='&'){
         code1=68;
         }
    else if (ch1=='*'){
         code1=69;
         }
    else if (ch1=='('){
         code1=70;
         }
    else if (ch1==')'){
         code1=71;
         }
    else if (ch1=='_'){
         code1=72;
         }
    else if (ch1=='-'){
         code1=73;
         }
    else if (ch1=='+'){
         code1=74;
         }
    else if (ch1=='='){
         code1=75;
         }
    else if (ch1=='?'){
         code1=76;
         }
    else if (ch1=='<'){
         code1=77;
         }
    else if (ch1=='>'){
         code1=78;
         }
    else if (ch1==':'){
         code1=79;
         }
    else if (ch1=='\\'){ // Escape Character \ needed to allow \ check
         code1=80;
         }
    else if (ch1=='/'){
         code1=81;
         }
    else if (ch1=='~'){
         code1=82;
         }
    else if (ch1=='.'){
         code1=83;
         }
    else if (ch1==','){
         code1=84;
         }
    else if (ch1==';'){
         code1=85;
         }
    else if (ch1=='\`'){ //Escape Character \ before ` to exit
         run=0; //Run = False at 0 and leaves while loop
         }
    else {
         cout<<"Invalid Input = No Match\n\n";
         }
         cout<<tab2[code1];




    myfile.open ("Crypt103Data.txt", ios::out | ios::app);
  myfile <<tab2[code1];
  myfile.close();

         }// end inner while loop
         //Add line return in file to seperate the data per line
             myfile.open ("Crypt103Data.txt", ios::out | ios::app);
  myfile <<"    "<<"\n";
  myfile.close();
  test=1;
  while(test==1){
          system("CLS");
          cout<<"Enter Y to continue or N to end \n\n\n";
          cin>>ch2;
          if (ch2=='N'||ch2=='n'){
                again=0;
                test=0;
                }
    else if (ch2=='Y'||ch2=='y'){
         again=1;
         test=0;
         run=1;
         system("CLS");
         }
    else {
         cout<<"Invalid Input, please choose Y or N \n\n";
         test=1;
         }   
         } 


} // end outter while loop

    system("CLS");
    system("PAUSE");
    return EXIT_SUCCESS;
}

asked 54 secs ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



Trying to figure out why when writing to file, the last character written is being duplicated when cout looks fine on display

Aucun commentaire:

Enregistrer un commentaire