0

I am calling std::cout as so:

 cout << "will " << tempLine << " be put in the table??" << endl; 

but my output is like this:

 be put in the table?? 

I feel like templine might have a '\0' character in it which stops std::cout working correctly. Would this cause said output?

I will put my code underneath for reference:

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <string> #include <map> using namespace std; int tempVarInst; int tempVarDest; int tempVarJmp = 0; int lineCount=0; map<string, int> symbolTable; bool replace(std::string& str, const std::string& from, const std::string& to) { size_t start_pos = str.find(from); if(start_pos == std::string::npos) return false; str.replace(start_pos, from.length(), to); return true; } bool isComment(string line){ string comment = "/"; if((line.find(comment)) != string::npos){ return true; }else{ return false; } } string ConvertToBinary(unsigned int val) { string tempStore; unsigned int mask = 1 << (sizeof(int) * 4 - 1); for(int i = 0; i < sizeof(int) * 4; i++){ if( (val & mask) == 0 ) tempStore+='0'; else tempStore+='1'; mask >>= 1; } return tempStore; } bool isMemLoc(string line){ string symbol = "@"; if(line.find(symbol) != string::npos ){ return true; }else{ return false; } } int destConstants(string line){ if(line== "A") return 32; if(line== "M") return 8; if(line== "D") return 16; if(line== "AM") return 40; if(line== "AD") return 48; if(line== "MD") return 24; if(line== "AMD") return 56; } int isInst(string line){ if(line=="0") return 60032; if(line=="1") return 61376; if(line=="-1") return 61056; if(line=="D") return 58112; if(line=="!D") return 58176; if(line=="M") return 64512; if(line=="A") return 60416; if(line=="!M") return 64576; if(line=="!A") return 60480; if(line=="-D") return 58304; if(line=="-M") return 64704; if(line=="-A") return 60608; if(line=="D+1") return 59328; if(line== "M+1") return 64960; if(line== "A+1") return 60864; if(line== "D-1") return 58240; if(line== "M-1") return 64640; if(line== "A-1") return 60544; if(line== "D+M") return 61568; if(line== "D+A") return 57472; if(line== "D-M") return 62656; if(line== "D-A") return 58560; if(line== "M-D") return 61888; if(line== "A-D") return 57792; if(line== "D&M") return 61440; if(line== "D&A") return 57344; if(line== "D|M") return 62784; if(line== "D|A") return 58688; } int jmpConstants(string line){ if(line== "JGT") return 1; if(line== "JEQ") return 2; if(line== "JGE") return 3; if(line== "JLT") return 4; if(line== "JNE") return 5; if(line== "JLE") return 6; if(line== "JMP") return 7; } bool isJumpInstruction(string line){ string symbol = ";"; if(line.find(symbol) != string::npos ){ return true; }else{ return false; } } void firstPass(ifstream &infile){ string tempLine; int iterator=0; while (getline(infile, tempLine)){ if(tempLine.substr(0,1) == "("){ cout << "will " << tempLine << " be put in the table??" << endl; //////////////////////////////////// for (map<string, int>::iterator it = symbolTable.begin(); it != symbolTable.end(); ++it) { cout << "ASDJASJASDJJSJAS: " << it->first << endl; if (it->first == tempLine) continue; else { symbolTable.insert(pair<string, int>(tempLine, lineCount)); lineCount++; } } ///////////////////////////// for (map<string, int>::iterator it = symbolTable.begin(); it != symbolTable.end(); ++it) { cout << "HERE The Symbol Table is: "<< it->first << " " << it->second << endl; } } else lineCount++; } cout << "end file" << endl; } int main( int argc, const char* argv[] ) { string outLine; string file1 = argv[1]; replace(file1, "asm", "hack"); //input //WHILE READ LINE() ifstream infile(argv[1]); string tempLine; ofstream outfile(file1.c_str()); tempVarJmp=0; tempVarDest=0; tempVarInst=0; firstPass(infile); ifstream secondPass(argv[1]); ofstream secondOut(file1.c_str()); cout << "hi" << endl; while (getline(secondPass, tempLine)){ //cout << "current line: " << tempLine << " length: " << tempLine.length() << endl; tempLine = tempLine.substr(0, tempLine.length()-1); //Blank check //cout << "The tempLine is: " << tempLine << endl; if(tempLine.length()<=1){ //cout << "Checking blackspaces, tempLine is: " << tempLine << endl; continue; } //Comment Check else if(isComment(tempLine)){ //cout << "Checking comments, tempLine is: " << tempLine << endl; continue; } //@ Check else if(isMemLoc(tempLine)){ tempLine.erase(0,1); int number = (atoi(tempLine.c_str())); //cout << "number: " << number << "binary: "<< ConvertToBinary(number) << endl; outfile << ConvertToBinary(number) << std::endl; continue; } //tempLine=tempLine(isInst).c_str+tempLine(destConstants)+tempLine(jmpConstants); //outfile << ConvertToBinary(tempLine) << endl; else if(isJumpInstruction(tempLine)){ // cout << "Jump Instruction" << endl; tempVarJmp = jmpConstants(tempLine.substr(tempLine.find(';')+1, tempLine.length())); cout << tempLine << " " << tempLine.length(); // cout << "TempVarInst: " << tempLine.substr(tempLine.find(';')+1) << endl; // cout << "tempVarJmp: " << tempVarJmp << endl; tempVarDest = isInst(tempLine.substr(0,tempLine.find(';'))); // cout << "tempvarDest = " << tempVarDest << endl; int finalVal=tempVarDest+tempVarJmp; outfile << ConvertToBinary(finalVal) << std::endl; /// cout << "TempVarJmp is: " << tempVarJmp << endl; // cout << "output should be: " << ConvertToBinary(finalVal) << endl; //cout << ConvertToBinary(finalVal) << endl; }else{ // cout << "Right TempLineInst is: " << tempLine.substr(tempLine.find('=')+1,tempLine.length()) << endl; // cout << "Right TempLineDest is: " << tempLine.substr(0,tempLine.find('=')) << endl; tempVarInst = isInst(tempLine.substr(tempLine.find('=')+1,tempLine.length())); tempVarDest = destConstants(tempLine.substr(0,tempLine.find('='))); tempVarJmp = 0; if(tempLine.substr(1,2) == ";J"){ tempVarJmp = jmpConstants(tempLine.substr(2,4)); // cout << "TempLineDest is: " << tempLine.substr(2,4) << endl; } //cout << "tempvarInst = " << tempVarInst << endl; //cout << "tempvarDest = " << tempVarDest << endl; //cout << "tempvarJmp = " << tempVarJmp << endl; int totalSum = tempVarInst+tempVarDest+tempVarJmp; //cout << "totalSum: " << ConvertToBinary(totalSum) << endl; outfile << ConvertToBinary(totalSum) << std::endl; } } outfile.close(); } 
7
  • It's likely the file has CRLF line endings and you're not on Windows. Commented Jun 6, 2015 at 3:35
  • 1
    What hapens when you hard-code the strings? Commented Jun 6, 2015 at 3:37
  • I am running on Red Hat Linux at the moment, is there an easy to fix this in code ie ignore CRLF line endings? Commented Jun 6, 2015 at 3:37
  • Hi Beta, I will check that for you now Commented Jun 6, 2015 at 3:37
  • @BarneyChambers, Remove carriage return characters from the (end of the) string? Commented Jun 6, 2015 at 3:38

1 Answer 1

4

Whether NUL could somehow prevent or clear input depends on your terminal program. A carriage return (ASCII code 13) is an even stronger candidate for the cause, as it's almost universally handled by terminals returning to the left-most column on the current line.

To find out, I suggest you write a function that outputs a string one character at a time, using escaping (in octal for control characters) much as you can use when specifying string literals in your program:

if (c == '"' || c == '\') os << '\\'; if (std::isprint(c)) os << c; else os << '\\' << std::setw(3) << std::setfill('0') << std::oct << (int)(unsigned char)c; 
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for a constructive way to analyze the cause of the problem. (breakpoint and watching the content of templine would also do the job,btw.)
What makes octal such a desirable choice here? Maybe it's me, but I'm not familiar with the octal representations of control characters at all.
@chris: good question - I have no idea why octal became popular, but now it's kind of traditional for C and consequently C++. A dozen or so characters have better known escape sequences like \r for carriage return and \t for a horizontal tab, but octal conveniently covers all ASCII characters. Support for hex is now required by the Standard ala \x12 - I'm not sure why they didn't add some notation for decimal too. Perhaps more people knowing their 8 times tables makes it easier than hex, if you do need to convert back to decimal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.