Code::Blocks Forums

User forums => Help => Topic started by: thehay95 on September 29, 2020, 07:02:43 pm

Title: Console Application doesn't work correctly
Post by: thehay95 on September 29, 2020, 07:02:43 pm
I'm having trouble with this encrypt-decrypt program, it compiles but it isn't saving(writing) nor reading the words on the .txt file, but the weird thing is this same code is working on Visual Studio, but as my teacher requires the use of Code::Blocks I'm here asking for help to discover why it isn't working on Code::Blocks.

Also, as I'm Brazilian, so some of the words of the code is in Portuguese, I'll try to give a all around idea of what each function does.

Code
#ifndef _ArquivoDados_
#define _ArquivoDados_

#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class arquivoDados
{
private:
    // attributes
fstream arquivo;
vector<string> vetor;
char caractere[2];

public:
    // Methods
// Standard builder
arquivoDados(){}

// Storage
void armazenar(char informacao, int num)
{
// Open the file with input mode and concatenation
arquivo.open("ArquivoDeDados.txt", fstream::in | fstream::app);

// Writing the information
if(informacao == '@') // If the information is an "@", turned into a string

arquivo << "&" << endl; // Place an "&" and jumps to another line

else
arquivo << informacao << ";"; // If not, put the information and divides the line with semicolon

// Stores the character
caractere[num] = informacao;

// Close File
arquivo.close();
}

// Storage
void armazenar(string informacao)
{
// Open the file with input mode and concatenation
arquivo.open("ArquivoDeDados.txt", fstream::in | fstream::app);

// Scans each letter of information
for(int i = 0; i < informacao.length(); i++)
if(informacao[i] == caractere[1]) //If it is equal to the second character
informacao[i] = caractere[0]; //  replaces by the first character

arquivo << informacao << ";"; // If not, put the information and divides the line with semicolon

// Close File
arquivo.close();
}

// Reading
void ler(int numLinha)
{
// Clean vector
vetor.clear();

// An auxiliary variable of type String
string temp = "", linha;

// Receive the entire line
// Open the file in output mode
arquivo.open("ArquivoDeDados.txt", fstream::out | fstream::in | fstream::app);

// Read an entire row
for(int j = 0; j < numLinha; j++)
getline(arquivo, linha);

// Close the file
arquivo.close();

// Reading loop
int i;
for(i = 0; i < linha.length(); i++) // Loop to read letter-by-letter
{
if(linha[i] == ';'){ // If the semicolon (end of word)
vetor.push_back(temp); // We store the string in the vector
temp = "";
}
else // If not
temp += linha[i]; // Increment the string with said letter
}
}

// Clear - Clears the TXT file
void clear()
{
// Open in "reading" and "replace modes
arquivo.open("ArquivoDeDados.txt", fstream::out | fstream::trunc);

// Closes the file
arquivo.close();
}

// Returns the number of lines
int quantidadeLinhas() {
// Auxiliary variables
int quantidade = 0; // Number of lines
string aux = "xxxxx"; // Auxiliary String to receive the lines

// Open the file with all modes
arquivo.open("ArquivoDeDados.txt", fstream::in | fstream::out | fstream::app);

// Until we get the empty line (last line)
while(aux != ""){
getline(arquivo, aux); // Reads a line (to check if it is empty)
quantidade++; // Increment the counter
}

// Closes the file to avoid losing data
arquivo.close();

// Returns the value of the entire amount minus 1
return quantidade - 1;
}

void exibir(){
// Loop to scan the vector
int i;
for(i = 0; i < quantidadeLinhas(); i++)
{
// Display the line number that will print
cout << "Pessoa numero " << i + 1 << ":" << endl; //Person Number

// Update vector "vetor" to each 'for' index "i"
ler(i + 1); // You have to put the "i + 1" because the index starts from 0, but the line starts from 1

for(int j = 2; j < vetor.size(); j++)
{
for(int g = 0; g < vetor[j].length(); g++) // Scan every letter of every word
if(vetor[j][g] == vetor[0][0]) // Replacing what is the first character
vetor[j][g] = vetor[1][0]; // For what is the second character

// And printing the word after the change
cout << vetor[j] << endl;
}
}
}
};

#endif
// Main function
int main()
{
// Creates an object of that class
arquivoDados objeto;

// Integer variable to the menu
int opcao = 0;

// Menu
while(opcao != 3)
{
// Opçoes
        cout << "\nMenu:"  << endl;
cout << "1. Gravar Dados"  << endl; // Record data
cout << "2. Exibir Dados"  << endl; // Exhibit data
cout << "3. Sair"  << endl; // Exit or end program
cout << "Digite sua Opcao: "; // Type your option
cin >> opcao; // option

// Switch-Case to treat the cases
switch(opcao)
{
case 1: // If the option to record data
// auxiliary variable
char caractere; // for the first two characters

// User input
cout << "Digite o primeiro caractere: "; //Enter the first character
cin >> caractere;

// Armazena o primeiro caracter e diz que é o primeiro caracter
objeto.armazenar(caractere, 0);

// Entrada do Usuário
cout << "Digite o segundo caractere: "; Type the second character
cin >> caractere;

// Stores the second character and says it is the second character
objeto.armazenar(caractere, 1);

// If the character is different from the "@" (end of line)
while(caractere != '@')
{
// We create a variable to receive the User Input
string informacao;

// Entrada do Usuário
cout << "Digite a informacao (ou arroba para terminar): "; // Enter the information (or at sign to end):
cin >> informacao;

if(informacao == "@") // If it is a "@"...
{
caractere = '@'; //We put the character as '@' (to stop the while)
objeto.armazenar(caractere, 0); // We store the object as a character to the existing check in method
}
else // If any text
objeto.armazenar(informacao); // We store this information
}
break;
case 2: // Exhibition option
// Activate exhibition method
objeto.exibir();
break;
case 3:
// End menu message
cout << "Ate mais"  << endl;
break;
default:
// If chosen option is invalided
cout << "Opcao inexistente" << endl;
break;
}
}

system("pause");
return 0;
}

If anyone has doubt any about a meaning of a word, just ask.
Thanks!!!
Title: Re: Console Application doesn't work correctly
Post by: Miguel Gimenez on September 29, 2020, 11:22:14 pm
You are trying to write data to a file opened for input (fstream::in), and trying to read from a file that is at EOF (fstream::app). This is not related to C::B, if you have further questions please address them to a forum where basic C++ programming is on topic.