I am required to read from an keyboard(stdin), the following text. Pl note that it will be entered by user from keyboard in this format only.
#the total size of physical memory (units are B, KB, MB, GB)
512MB 2 #the following are memory allocations
{
abc = alloc(1KB);
{
y_ = alloc(128MB);
x1= alloc(128MB);
y_ = alloc(32MB);
for (i = 0; i < 256; i++) abc[i] =alloc(512kB);
x1 = alloc(32MB); x2 = alloc(32MB); x3 = alloc(32MB);
x1.next = x2, x2.next = x3, x3.next = x1;
}
abc = alloc(256MB);
}
So basically let me break it down.
a line beginning with # sign is considered as comment and is ignored.
the first two allocations are physical memory size and number of generations.
a global bracket will be opened.
and it may be followed by a line called
abc = alloc(1KB);
where abc is the object name, 1KB is the memory size allocated.
x1.next = x2, here x1 points to x2.
for (i = 0; i < 256; i++) abc[i] =alloc(512kB);
the for loop os entered in this format and it can have a same line command or can have nested for loops.
I have the following code that takes care of this somewhat. I want to know improve on it. Please do help.
And my code is this:
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <limits>
#include <stdio.h>
#include <sstream>
using namespace std;
using std::stringstream;
string pMem,sGen, comment,val,input,input_for,id_size,id,init_str1, init_str2, inc_str, id_dummy,s_out,sss, id_dummy1;
int gen=0, pMem_int=0,i=0, gBrckt =0,cBrckt=0, oBrckt=0, id_size_int,v1,v2, for_oBrckt=0,for_cBrckt=0,y=0, y1=0, g=0;
unsigned long pMem_ulong =0, id_size_ulong;
char t[20], m[256], init1[10],init2[10],inc[10];
unsigned pos_start, pos,pos_strt=0,pos_end=0;
string extract(string pMem_extract);
unsigned long toByte(int pMem_int_func, string val);
void commentIgnore(string& input);
void func_insert();
void func_insert_for();
stringstream out;
void commentIgnore_for(string& input_for);
int main()
{
/* Reading the input main memory and num of generations */
/* Ignoring comment line */
cin >> pMem;
if(pMem == "#") {
cin.clear();
pMem.clear();
getline(cin,comment);
cin >> pMem;
}
if(pMem == "#") {
cin.clear();
pMem.clear();
getline(cin,comment);
cin >> pMem;
}
if(pMem == "#") {
cin.clear();
pMem.clear();
getline(cin,comment);
cin >> pMem;
}
/* Reading input generations */
cin>> sGen;
if(sGen == "#") {
cin.clear();
sGen.clear();
getline(cin,comment);
cin >> sGen;
}
if(sGen == "#") {
cin.clear();
sGen.clear();
getline(cin,comment);
cin >> sGen;
}
if(sGen == "#") {
cin.clear();
sGen.clear();
getline(cin,comment);
cin >> sGen;
}
/* Convert sGen and physical memory to int and report error if not a number */
gen = atoi(sGen.c_str());
if(gen ==0) {
cerr << "Generation must be a number"<<endl;
exit(0);
}
pMem_int = atoi(pMem.c_str());
// cout<< gen<<" "<<pMem_int<<endl;
/* Now that the number from pMem is removed, get its unit B,MB,KB */
extract(pMem); /* returns val(string) */
/* convert the given physical memory to Byte. input: pMem_int*/
toByte(pMem_int, val); /* return(pMem_ulong)*/
// move pMem_ulond to another location to keep address intact
/* read rest of the inputs */
/* Ignore comment lines before the global bracket */
cin >> input;
if(input == "#"){
cin.clear();
input.clear();
getline(cin,comment);
cin >> input;
}
if(input == "#"){
cin.clear();
input.clear();
getline(cin,comment);
cin >> input;
}
if(input == "#"){
cin.clear();
input.clear();
getline(cin,comment);
cin >> input;
}
if(input.compare("{") ==0)
gBrckt=1;
else {
cerr<< "Syntax error\n";
exit(0);
}
/* Clearing the input stream for next input */
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.clear();
input.clear();
//cout<<"input: "<<input<<endl;
while( getline(cin,input)) {
if(input == "CTRL-D")
break;
commentIgnore(input);
//cout<<"inputloop: "<<input<<endl;
/* If input = '{' or '}'*/
if(input.compare("{") ==0)
oBrckt = oBrckt + 1;
if (input.compare("}") ==0)
cBrckt = cBrckt + 1;
if (((input.find("alloc"))!= string::npos) && (input.find("alloc") < input.find("for"))) {
func_insert();
//call the allocate function here with name: id, size: id_size_ulong
}
if ((input.find("for")) != string::npos) {
sscanf(input.c_str(), "for (%s = %d; %s < %d; %[^)])", init1, &v1, init2, &v2, inc);
init_str1 = init1, init_str2 = init2, inc_str = inc;
cout<<init1<<" ="<< v1<<" "<<init_str1<<" < " << v2<< " "<< inc_str<<endl;
cout << input <<endl;
if(init_str1 != init_str2) {
cerr << "Error!\n";
exit(0);
}
if ((input.find("alloc"))!= string::npos) {
// unsigned pos = (input.find("alloc"));
if((input.find(";")) != string::npos) {
pos_start = (input.find(")")+1);
string alloc_substr = input.substr(pos_start);
cout<<"Substring alloc: "<< alloc_substr<<endl;
func_insert();
//call the allocate function here with name: id, size: id_size_ulong
}
else {
cerr << "ERROR: SYNTAX\n";
exit(0);
}
}
// cin.ignore();
while(getline(cin,input_for)) {
commentIgnore_for(input_for);
if ((input_for.find("{") != string::npos)) {
pos = input_for.find("{");
for_oBrckt = for_oBrckt+1;
string for_brckt = input_for.substr(pos,pos);
cout<< "Found: " << for_oBrckt<<endl;
}
if ((input_for.find("}") != string::npos)) {
pos = input_for.find("}");
for_cBrckt = for_cBrckt+1;
string for_brckt = input_for.substr(pos,pos);
cout<< "Found: " << for_cBrckt<<endl;
}
if (((input_for.find("alloc"))!= string::npos) && (input_for.find("alloc") < input_for.find("for"))) {
func_insert_for();
//call the allocate function here with name: id, size: id_size_ulong
}
if(for_oBrckt == for_cBrckt)
break;
}
cout<<"out of break"<<endl;
}
if (((input.find(".next"))!= string::npos) && (input.find(".next") < input.find("for"))) {
func_insert();
//call the allocate function here with name: id, size: id_size_ulong
}
if(((cBrckt-oBrckt)) == gBrckt)
break;
}
}
/*---------------------- Function definitions --------------------------------*/
/* Function to extract the string part of physical memory */
string extract(string pMem_extract) {
i=0;
const char *p = pMem_extract.c_str();
for(i=0; i<=(pMem_extract.length()); i++) {
if (*p=='0'|| *p=='1'|| *p=='2'|| *p=='3'|| *p =='4'|| *p=='5'|| *p=='6'|| *p=='7'|| *p=='8'|| *p=='9')
*p++;
else {
val = pMem_extract.substr(i);
return(val);
}
}
}
/* Convert the physical memory to bytes. return(pMem_ulong);*/
unsigned long toByte(int pMem_int_func, string val)
{
if (val == "KB")
pMem_ulong = (unsigned long) pMem_int_func * 1024;
else if (val == "B")
pMem_ulong = (unsigned long) pMem_int_func;
else if (val == "GB")
pMem_ulong = (unsigned long) pMem_int_func * 1073741824;
else if (val == "MB")
pMem_ulong = (unsigned long) pMem_int_func * 1048576;
else {
cerr<<"Missing the value in memory, B, KB, MB, GB\n";
exit(0);
}
return(pMem_ulong);
}
/*Ignoring comment line*/
void commentIgnore(string& input)
{
unsigned found = input.find('#');
if (found!=std::string::npos)
input= input.erase(found);
else
return;
return;
}
void func_insert() {
sscanf(input.c_str(), "%s = alloc(%[^)]);", t, m);
id =t;
id_size =m;
cout<<"Tag: "<<id <<" Memory: "<<id_size<<endl;
extract(id_size); /* Separates B,MB,KB and GB of input, returns val*/
id_size_int = atoi(id_size.c_str());
/* Convert object size to B */
toByte(id_size_int, val); /* return(pMem_ulong) */
id_size_ulong = pMem_ulong;
}
void func_insert_for() {
sscanf(input_for.c_str(), "%s = alloc(%[^)]);", t, m);
id =t;
id_size =m;
if(!((id.find("[")) && (id.find("]")) != string::npos)) {
cout<<"Tag: "<<id <<" Memory: "<<id_size<<endl;
extract(id_size); /* Separates B,MB,KB and GB of input, returns val*/
id_size_int = atoi(id_size.c_str());
/* Convert object size to B */
toByte(id_size_int, val); /* return(pMem_ulong) */
id_size_ulong = pMem_ulong;
// allocate here
return;
}
else {
if(inc_str.find("++"))
y1 =1;
if(inc_str.find("="))
{
sss = inc_str.substr(inc_str.find("+") +1);
y1 = atoi(sss.c_str());
cout<<"y1:"<<y1<<endl;
}
pos_strt = id.find("[");
pos_end = id.find("]") -1;
cout<<"Positions start and ebd: " << pos_strt<<pos_end<<endl;
id_dummy = id.substr(0,pos_strt);
id = id_dummy;
cout<<"Tag: "<<id_dummy <<" Memory: "<<id_size<<endl;
extract(id_size); /* Separates B,MB,KB and GB of input, returns val*/
id_size_int = atoi(id_size.c_str());
/* Convert object size to B */
toByte(id_size_int, val); /* return(pMem_ulong) */
id_size_ulong = pMem_ulong;
//allocate here
cout<<"v1: " << v1 << " " << v2<<endl;
// g = 0;
for(y = v1; y < v2; y= y+y1) {
// allocate here
}
}
return;
}
void commentIgnore_for(string& input_for)
{
unsigned found = input_for.find('#');
if (found!=std::string::npos)
input_for= input_for.erase(found);
else
return;
return;
}
Also i am required to make it whitespace compatible. What it means is that input can be entered in one line as well. like two allocations in one line. Which i have not been able to taken care. I need help with that.
My suggestion would be that you write a proper tokenizer - a piece of code that understands what belongs together (such as "words"), and where to split (e.g. "(, ), {, }"). The tokenizer would return an enum, something like this:
enum Token {
Token_Unknown, // Error indiciation.
Token_LeftParen,
Token_RightParen,
Token_LeftBracket,
Token_RightBracket,
Token_Comma,
Token_Semicolon,
Token_Equal,
Token_Word, // Sequence of alphanumerics
};
Once you have a token, you need to understand what that token is. It may make sense to have a table of "known" tokens (aka "keywords"), such as "mem", "alloc", "for", and so on. If something isn't a keyword, it's the name of a symbol, such as a variable. You store those in a table, so that you can refer to them later.
You will have to use some sort of stack of where you are, so you can get back to where you came from when you finish.
Writing a generic parser isn't terribly hard, and you will most likely end up with a lot less code than your current code in doing so.
Certainly, you'd be able to get rid of all the:
if(input == "#"){
cin.clear();
input.clear();
getline(cin,comment);
cin >> input;
}
Just let the parser check if there is a '#' as the input, and skip over to the end of the line (completing the current token if you have one, if not, just keep going).
IIUC, the input is not line oriented, so the usual rule of
using std::getline may not apply. My own approach would be:
Insert a filtering streambuf to removed the comments.
Use some sort of regular expression based lexer (e.g. flex)
to break the input up into tokens.
Define the grammar; I'd use bison once I'd gotten this far,
in order to generate the code to parse the grammar, but a simple
recursive descent parser shouldn't be too hard to write.
You don't say what you must do with the information once you
have parsed it, so it's hard to give more precise advice.
Related
We need to create a universal Turing machine.
We have a file with the provided information: tape count, starting input, starting position and the rules.
Reading from the file isn't that big of a problem. What I'm doing right now is creating a structure vector and reading all the rules to there.
What I can't figure out is how to make the machine itself work. We know that the starting state is always zero, so I'm starting from there. Looking for the rules that start with this state, but what I cannot figure out is what if I have to jump back to the first rules? What to do then? What can of counting mechanism for the vector can I implement? I'm really new to vectors. I'll add that for the algorithm itself.
No more than two loops can be used, not counting printing out text or reading from the file.
The code I have right now is:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <Windows.h>
struct rule {
std::string qstate; // dabartine busena
char csymbol; // dabartinis simbolis
char nsymbol; // naujasis simbolis
char direction; // i kuria puse eis galvute
std::string nstate; // naujoji busena
};
void printingText(std::vector<char> input, int position, long long steps);
void searchingForASymbolOrState(std::vector<rule> rules, std::vector<char> input, std::string state, int position, int& cursorPos);
int main()
{
int tapeCount, position;
long long steps = 0;
std::string tape;
std::ifstream file("1.txt");
file >> tapeCount >> tape >> position;
std::vector <char> input(tape.begin(), tape.end());
std::vector <rule> rules;
rule temp;
while (file >> temp.qstate) {
file >> temp.csymbol;
file >> temp.nsymbol;
file >> temp.direction;
file >> temp.nstate;
rules.push_back(temp);
}
file.close();
position--; // kadangi masyvas skaiciuoja nuo nulio, tai ir startine pozicija sumazinu, kadangi ji skaiciuoja nuo vieno
int cursorPos = 0; // saugosim vieta, kurioje vietoje prasideda taisykles su reikiama busena
std::string state = "0"; // saugosim busena, kad zinotume, kokioje busenoje siuo metu esame
// Tiuringo masinos "algoritmas"
while (true) {
printingText(input, position, steps);
if (state == rules[cursorPos].qstate) {
if (input[position] == rules[cursorPos].csymbol) {
if (input[position] != rules[cursorPos].nsymbol) {
input[position] = rules[cursorPos].nsymbol;
if (rules[cursorPos].direction == 'L') {
position--;
steps++;
}
else if (rules[cursorPos].direction == 'R') {
position++;
steps++;
}
if (rules[cursorPos].nstate != state) {
state = rules[cursorPos].nstate;
}
}
else if (input[position] == rules[cursorPos].nsymbol) {
if (rules[cursorPos].direction == 'L') {
position--;
steps++;
}
else if (rules[cursorPos].direction == 'R') {
position++;
steps++;
}
if (rules[cursorPos].nstate != state) {
state = rules[cursorPos].nstate;
}
}
}
else if (input[position] != rules[cursorPos].csymbol) {
searchingForASymbolOrState(rules, input, state, position, cursorPos);
}
}
else if (state != rules[cursorPos].qstate) {
searchingForASymbolOrState(rules, input, state, position, cursorPos);
} // Skaiciuojam zingsnius
// std::cout << cursorPos << " " << position << " " << state << " " << rules[cursorPos].qstate; // Eilute naudojama klaidu paieskai
Sleep(100);
system("cls");
}
// "Algoritmo pabaiga"
}
void printingText(std::vector<char> input, int position, long long steps) {
std::cout << "Head position can be seen with '' symbols\n\n";
for (int i = 0; i < input.size(); i++) {
if (i == position) {
std::cout << "'" << input[i] << "'";
}
else {
std::cout << input[i];
}
}
std::cout << "\n\nSteps: " << steps;
}
void searchingForASymbolOrState(std::vector<rule> rules, std::vector<char> input, std::string state, int position, int& cursorPos) {
for (int i = 0; i < rules.size(); i++) {
if (rules[i].qstate == state) {
if (rules[i].csymbol == input[position]) {
cursorPos = i;
}
}
if (rules[cursorPos].qstate != state) {
if (rules[i].qstate == state) {
cursorPos = i;
}
}
}
}
I know that either .eof() or system("cls") aren't good functions to use, but for this project, I think they'll work fine. Correct me if I'm wrong.
EDIT: I tried to do something. Not sure if there's a more effective why. Obviously it's not finished, no halting and error checking and etc. But if you have any comments, they would be really appreciated.
I have to write a programs that takes an input of string which has some '$' and digits. The output of the program is set of all possible strings where the '$ in the string is replaced by all the other digits.
I have written the following code for it.
#include<bits/stdc++.h>
using namespace std;
int numberOf(string in)
{
int count = 0;
for(int i = 0; i <= in.size()-1; i++)
if(in[i] == '$')
count++;
return count;
}
void solve(string in, string in1, vector <string> &s,
int index)
{
if(numberOf(in) == 0)
{
s.push_back(in);
return;
}
if(index == in.size())
{
return;
}
if(in1.empty())
{
return;
}
else
{
if(in[index] == '$')
{
string in2 = in;
in2[index] = in1[0];
string in3 = in1;
in3.erase(in3.begin());
solve(in2, in1, s, index+1);
solve(in, in3, s, index);
return;
}
else
{
solve(in, in1, s, index+1);
return;
}
}
}
void replaceDollar(string in)
{
string in1 = in;
int count = 0;
for(int i = 0; i <= in.size()- 1; i++)
{
if(in[i] != '$')
{
in1.push_back(in[i]);
count++;
}
}
count = in.size() - count;
cout << "Number is " << count << "\n";
vector <string> s;
solve(in, in1, s, 0);
for(auto i = s.begin(); i != s.end(); i++)
cout << *i << " ";
cout << "\n";
}
int main()
{
int t;
cin >> t;
while(t--)
{
string in;
cin >> in;
replaceDollar(in);
}
return 0;
}
For following input
1
$45
The expected output should be
445 545
But it returns
445 545 445 545
Can anyone please explain why is it outputting repeated strings?
Also can anyone suggest a better approach to this question?
Thanks in advance!
Assuming that this is homework:
Start over. Your code is way too complex for this problem.
I would treat everything as type char
Loop/iterate over said string, using std::string::replace() to replace each instance of $ with each digit.
-- If your teacher doesn't want you using std libraries, then add another loop and compare yourself.
3a. Of course, add a check, so that you don't replace $ with $
Create a new copy of the string on each iteration.
Print each to stdout as you create them.
See this post:
How to replace all occurrences of a character in string?
p.s. Pro tip: don't use using namespace. Use the full namespace in your calls; e.g.:
Bad
using namespace std;
string = "hello world";
Good
std::string = "hello world";
sorry for such a stupid question but I couldn't find any obvious answer.
I need to read from stdin first an int n with the size of an array, and then integer values from a string in the format "1 2 3 4 5 6" with n elements.
If I knew the number of parameters at compile time I could use something like a scanf (or the safe alternatives) with a format string like "%d %d %d %d %d %d", but here I will only know that value at run time.
What would be the best way to do this in C++? Performance is important but more than that safety.
How should I read a format string of variable length in C++ from stdin?
You should not attempt to do such thing. Only ever use constant format strings.
I need to read from stdin first an int n with the size of an array, and then integer values
What would be the best way to do this in C++?
Read one value at a time. Repeat using a loop.
Here's a function that does what errorika describes:
const int SIZE = //as much of your memory as you'd like the user to have access to
***caller function must include this:
//allocate a string to hold some data;
char* buffer = NULL;
buffer = malloc (SIZE * sizeof(char));
if (buffer == NULL) {
printf("malloc error terminating\n");
return;
}
***
void getEntry(char* buffer) {
int count = 0;
int maxlen = SIZE - 1;
char a = '0';
for (int i = 0; i < SIZE; i++) {
buffer[i] = '0';
}
while (a != '\n' && count < maxlen) {
a = fgetc(stdin);
buffer[count] = a;
count++;
}
if (a == '\n') {
buffer[count - 1] = '\0';
}
else {
buffer[count] = '\0';
do {
a = fgetc(stdin);
} while (a != '\n');
}
}
This is all basic C code but user entry is evil. Here is what I've come up with for more C++ idiomatic user input functions (query is just the message string you pass in):
template<typename T>
void getInput(const std::string query, T& entry) {
std::string input;
std::cout << query << std::endl;
getline(std::cin, input);
std::stringstream buffer{input};
buffer >> entry;
}
OR
template<typename T>
void getInput2(std::string query, T& entry) {
bool validInput = false;
while (validInput == false)
{
validInput = true;
std::cout << query << std::endl;
std::cin >> entry;
if (std::cin.fail()) {
validInput = false;
std::cout << "Unacceptable entry\n" << std::endl;
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
For a project, we have to make a Caesar Cipher using classes and save the encrypted message inside of a file so a user can decipher it with the program.
I input the message and it has no problem encrypting the message according to the displacement/key I input (since I gave an option for the user to place the displacement they please).
However, the problem lies in decripting the message. It seems to only decript the penultimate or last letter of what I inputted and it doesnt even bother to show the remaining characters of the message.
I have currently no idea why its acting the way it is, I figured I would have to change the message to take char variables instead of string, but that would mean rewriting a large chunk of the code, and at the moment, I would like to avoid having to rewrite the code from scratch. If there are no other options, then I guess I will have to rewrite the code.
Here is the code, (hope that helps and sorry if my message may seem messy, this is the first time I post anything here):
#include<iostream>
#include<string>
#include<fstream>
#include<ctime>
#include<cstdlib>
/* This is a program that will grab a message from the user and encrypt it, then decrypt it
It will also generate a random 8-digit character password used to access the encrypted file
It will also have the ability to allow the user to choose how many spaces the cipher will take into account */
using namespace std;
//Implement a set displacement and get displacement
class Cipherer
{
private:
int displacement;
string message;
//string decryptMessage;
public:
void setDisplacer(int key);
int getDisplacer()const;
void msgEncripter(string, int);
string getMessage()const;
void msgDecripter(string);
string getDecription()const;
};
void Cipherer::setDisplacer(int key)
{
displacement = key;
}
int Cipherer::getDisplacer()const
{
return displacement;
}
void Cipherer::msgEncripter(string msg, int key)
{
string encriptedMsg = msg;
//.size returns the number of elements
for (unsigned int i = 0; i < msg.size(); i++)
{
if (msg[i] == 32) //32 is the value in ASCII of the space character
{
continue;
}
else
{
if ((msg[i] + key) > 122)
{
int temp = (msg[i] + key) - 122;
encriptedMsg[i] = 96 + temp;
}
else if (msg[i] + key > 90 && msg[i] <= 96)
{
int temp = (msg[i] + key) - 90;
encriptedMsg[i] = 64 + temp;
}
else
{
encriptedMsg[i] += key;
}
}
}
message = encriptedMsg;
}
string Cipherer::getMessage()const
{
return message;
}
void Cipherer::msgDecripter(string msg)
{
string decriptedMsg;
for (unsigned int i = 0; i < msg.size(); i++)
{
if (msg[i] == 32)
{
continue;
}
else
{
if ((msg[i] - displacement) < 97 && (msg[i] - displacement) > 90)
{
decriptedMsg[i] = (msg[i] - displacement) + 26;
}
else if ((msg[i] - displacement) < 65)
{
decriptedMsg[i] = (msg[i] - displacement) + 26;
}
else
{
decriptedMsg = msg[i] - displacement;
}
}
}
message = decriptedMsg;
}
string Cipherer::getDecription()const
{
return message;
}
static const char PASSWORD_POOL[] =
"0123456789";
int poolSize = sizeof(PASSWORD_POOL) - 1;
char getRandChar()
{
return PASSWORD_POOL[rand() % poolSize];
}
int main()
{
srand(time(0));
string pass, input, msg;
int key;
Cipherer message;
ofstream outputFile;
ifstream inputFile;
outputFile.open("SecretMSG.txt");
cout << "Write a message: \n";
getline(cin, msg);
cout << "Choose the displacement of the message (0-25): ";
cin >> key;
message.setDisplacer(key);
message.msgEncripter(msg, key);
outputFile << msg;
outputFile.close();
for (int count = 0; count < 1; count++)
{
for (int i = 0; i <= 7; i++)
{
pass += getRandChar();
}
cout << pass << endl;
}
cout << "Input password " << pass << " ";
cin >> input;
if (input == pass)
{
//Make a local variable to read file
string encryptedMessage;
inputFile.open("SecretMSG.txt");
inputFile >> encryptedMessage;
inputFile.close();
cout << message.getMessage() << endl;
cout << "If you wish to decrypt the message, type in the password once again " << pass << ": ";
cin >> input;
if (input == pass)
{
message.msgDecripter(encryptedMessage);
cout << message.getDecription() << endl;
}
else
{
exit(EXIT_FAILURE);
}
}
else
{
exit(EXIT_FAILURE);
}
system("pause");
return 0;
}
In msgDecripter your string decriptedMsg creates a string with the size 0, so any decriptedMsg[i] = is undefined behavior.
In your msgEncripter you write string encriptedMsg = msg;, and because you create a copy of mgs the encriptedMsg has the same size.
So either you do string decriptedMsg = msg or string decriptedMsg = std::string(msg.size(), ' ');
But a more c++ like approach would be to use transform.
string encriptedMsg = msg;
std::transform(encriptedMsg.begin(), encriptedMsg.end(), encriptedMsg.begin(),
[](unsigned char c) -> unsigned char {
if( c == ' ') {
return c;
} else {
// ... your other encrypting logic ...
}
});
Or using msg as source and an empty string as target and utilize std::back_inserter.
This is an assignment that required me to use ifstream to stream a CSV file. this csv file contains 52 state names and amount of different resources used by each state. for example:
Alabama,410.20,715.70,169.40,18.00,44.90,309.10,11.90,417.30,64.50,167.40,23.70,0.10,0.40,0.00
then I need to prompt the user to type the state name and the output is the percentage of resources used.
I created a struct containing a string type and an array to store the value of each state and created an array of struct to store multiple state's data, but I am not sure whether my code is right, and I want to know how to access other data, such as the data store in my double array, when the user input a state name.
here is my code:
struct statData
{
string statename;
double StatDataNumber[14];
}DataStruc[51];
int main()
{
ifstream indata;
string line;
statData State;
State.statename;
statData Consumption;
Consumption.StatDataNumber;
indata.open("Data2016.csv"); //opening file
if (indata.fail()) //fail safe
{
cout << "Fail to open file";
exit(1);
}
getline(indata, line); //skipping the first line of the csv file
int i;
int N = 0;
int NLoop;
int Loop = 0;
string InvertValueBefore;
double InvertValueAfter;
char comma;
while (indata.eof()) // before file reache the end
{
for (NLoop = 0; NLoop < 51; NLoop++) // struct array loop
{
{
getline(indata, DataStruc[Loop].statename, ',');// getting statename
for (i = 0; i <= 12; i++) // each group of data, except last
{
indata >> DataStruc[Loop].StatDataNumber[N] >> comma;// storing data in struct
N++;
}
getline(indata, InvertValueBefore); // store last value as string
InvertValueAfter = stoi(InvertValueBefore); // convert it into double
InvertValueAfter = DataStruc[Loop].StatDataNumber[N]; // store it in array of struct
}
Loop++;
}
}
ReadData();
return 0;
}
void ReadData (ifstream& indata , statData DataStruc[] )
{
int i;
string input;
bool stayinloop = true;
cout << "Enter a statename or 'q' to quit\n";
getline(cin, input);
while (stayinloop == true)
{
if (input == "Alabama")
DataStruc[i].statename == "Alabama";
DataStruc[i].StatDataNumber[]
}
}
this code is not finished. Please let me know if you spot any other error. Thank you!
Your code is fine. However, certain points:
1. You just need to get rid of certain variables which are not required.
2. The "eof" function is used to identify if the end of file is reached. For which, you need to use while (!indata.eof()).
3. The "ReadData" method should appear before the main function, however, if you want to have your functions after the main function then first you need to define your function declaration before the main function (i.e. before main function, you can put "void ReadData (ifstream& indata , statData DataStruc[]);"), afterwards you can define your function.
Below is a working version of your requirements.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
struct statData
{
string statename;
double StatDataNumber[3];
}DataStruc[2];
void ReadData (ifstream& indata , statData DataStruc[])
{
string input;
bool stayinloop = true;
while (stayinloop)
{
cout << "\nEnter a statename or 'q' to quit\n";
getline(cin, input);
for (int i = 0 ; i < 2; i++)
{
if (input == DataStruc[i].statename)
{
for(int j = 0 ; j < 3; j++)
{
cout << DataStruc[i].StatDataNumber[j] << ',';
}
}
else if(input == "q")
{
stayinloop = false;
}
}
}
}
int main()
{
ifstream indata;
string tempData = "";
string line;
string InvertValueBefore = "";
double InvertValueAfter = 0.0;
char comma = ',';
indata.open("test.csv"); //opening file
if (indata.fail()) //fail safe
{
cout << "Fail to open file";
}
getline(indata, line); //skipping the first line of the csv file
while (!indata.eof()) // before file reach the end
{
for (int NLoop = 0; NLoop < 2; NLoop++) // struct array loop
{
{
getline(indata, DataStruc[NLoop].statename, comma);// getting statename
for (int i = 0; i < 2; i++) // each group of data, except last
{
getline(indata, tempData, comma);
DataStruc[NLoop].StatDataNumber[i] = atof(tempData.c_str());
}
getline(indata, InvertValueBefore); // store last value as string
InvertValueAfter = atof(InvertValueBefore.c_str()); // convert it into double
DataStruc[NLoop].StatDataNumber[2] = InvertValueAfter;
}
}
}
ReadData(indata, DataStruc);
return 0;
}