Getting only partial output - c++

I wrote a C++ program to print out the multiplication of all two digit numbers.
I am getting only partial output of some product and not the entire output.
Code:
int main()
{
int ans;
for (int i = 10; i <= 99; i++) {
for (int j = 10; j <= 99; j++) {
ans = i * j;
cout << ans << endl;
}
}
cin.get();
return 0;
}
The output starts from 6816 (instead of 10*10=100) upto 9801 (which is 99*99).
On the other hand if I do the same for single digits, the output is correct.

There are 299 numbers between 6801 and 9899 (both inclusive) printed from this program, so it seems you are using Command Prompt of Windows to view the result.
If so, to view all numbers in the window,
Right-click the window icon in upper left
Select "property"
Select "layout" tab
Set "height" in "buffer size of screen" to a large number such as "9999".
(The name of menus might not be correct because I am using Japanese OS)
Alternatively, you might want to use redirect to put the output into a text file.

Aside from missing #include and using namespace std; lines you did not post, there is nothing wrong with your program.
The output is 8100 lines long, are you sure you can see all these lines in your terminal? Try redirecting the output to a file and load this file into an editor to verify the number of lines.

Related

C++ the first cout will not be printed

As you can see, I have a cout here before the loop starts. Seekg starts at the beginning and the first word in the txtfile is "Hello". But it doesn't print it out(the first cout).
When I remove the while loop, then its not a problem, the first word gets included in the output.
The first "cout", shouldn't it be printed out no matter what comes after it? How is it possible that next lines of code(THIS case) affects the previous "cout" above it ?
int main()
{
string a;
ifstream myfile;
myfile.open("test.txt");
myfile.seekg(0);
getline(myfile,a);
cout << a << endl;
int z = 0;
while( a != "x" ) {
myfile.seekg(z);
getline(myfile,a);
z += a.size() + 2;
cout << a << endl;
}
}
I Also should mention that the file starts with "hello", AND contains 600 newlines and a word on every new line. The last line contains "x".
[EDIT]
Here's another example. I belive this will be helpful. This is my code now:
int main() {
string a;
ifstream myfile;
int newpos = 0;
myfile.open("example.txt");
myfile.seekg(newpos);
getline(myfile,a);
while (a != "x") {
myfile.seekg(newpos);
getline(myfile,a);
cout << a << endl;
newpos += a.size() + 2;
}
}
This is my textfile:
hello
johan
nils
erik
john
x
The output is exactly as it looks in the textfile above.(so far so good).
BUT, then I add about 600 new names to the list/textfile(same structure as above, no changes there, just new names added). Only ONE "hello" at the top so I can track the start, and one x at the end.
This still works with around 100 new names, hello is still printed out in the beginning. But when i add around 600 new names , hello will not be included(the very first cout). Why is that?
But when I add around 600 new names, hello will not be included (the very first cout).Why is that?
Looks like your Screen Buffer Size wasn't big enough to show all of the buffer lines. So when the number of printed lines exceeds a specific height, the console starts to ignore the previously printed lines which result you think it didn't print them all.
Since you're using Windows you can try to increase it as so
For all console apps
Right-click the title of the console and choose Defaults
go to Layout tab
Under the Screen Buffer Size, Increase the Height to (i.e 9001)
Click OK then restart the console
For a specific app
Right-click the title of the console and choose Properties
go to Layout tab
Under the Screen Buffer Size, Increase the Height to (i.e 9001)
Click OK.
Note that the Default Height in most Windows Operating systems is 9001.

C++ std::cin is stuck at 80 lines of input

here is the code that I have
int main()
{
int total = 0;
int count = 0;
std::cin >> total;
int arr[4] = {0,0,0,0};
while(count < total)
{
std::cin>>arr[0]>>arr[1]>>arr[2]>>arr[3];
count++;
std::cout<<count<<std::endl;
}
return 0;
}
so the first line of input tells how many lines I need to read after..and each line has 4 numbers separated by space. Whenever the number of lines exceed 80(e.g 100), then the while loop gets stuck. I have no idea what causes the problem and I have tried a couple things like cin.clear() but they just didnt work....
Edit: std::cin stops reading after 80 lines of input with format like 10 20 210 10
Xcode with LLVM didn't work...However g++ using terminal works.... http://melpon.org/wandbox/permlink/UXAMgM4ldn2K2NgU
here is the code that works on my terminal with g++ but not my xcode...
It's the output that's getting stuck. Unless the output of the count is being read by something and consumed, eventually the output buffer will get full and the cout line will block.

Logic for reading rows and columns from a text file (textparser) C++

I'm really stuck with this problem I'm having for reading rows and columns from a text file. We're using text files that our prof gave us. I have the functionality running so when the user in puts "numrows (file)" the number of rows in that file prints out.
However, every time I enter the text files, it's giving me 19 for both. The first text file only has 4 rows and the other one has 7. I know my logic is wrong, but I have no idea how to fix it.
Here's what I have for the numrows function:
int numrows(string line) {
ifstream ifs;
int i;
int row = 0;
int array [10] = {0};
while (ifs.good()) {
while (getline(ifs, line)) {
istringstream stream(line);
row = 0;
while(stream >>i) {
array[row] = i;
row++;
}
}
}
}
and here's the numcols:
int numcols(string line) {
int col = 0;
int i;
int arrayA[10] = {0};
ifstream ifs;
while (ifs.good()) {
istringstream streamA(line);
col = 0;
while (streamA >>i){
arrayA[col] = i;
col++;
}
}
}
edit: #chris yes, I wasn't sure what value to return as well. Here's my main:
int main() {
string fname, line;
ifstream ifs;
cout << "---- Enter a file name : ";
while (getline(cin, fname)) { // Ctrl-Z/D to quit!
// tries to open the file whose name is in string fname
ifs.open(fname.c_str());
if(fname.substr(0,8)=="numrows ") {
line.clear();
for (int i = 8; i<fname.length(); i++) {
line = line+fname[i];
}
cout << numrows (line) << endl;
ifs.close();
}
}
return 0;
}
This problem can be more easily solved by opening the text file as an ifstream, and then using std::get to process your input.
You can try for comparison against '\n' as the end of line character, and implement a pair of counters, one for columns on a line, the other for lines.
If you have variable length columns, you might want to store the values of (numColumns in a line) in a std::vector<int>, using myVector.push_back(numColumns) or similar.
Both links are to the cplusplus.com/reference section, which can provide a large amount of information about C++ and the STL.
Edited-in overview of possible workflow
You want one program, which will take a filename, and an 'operation', in this case "numrows" or "numcols". As such, your first steps are to find out the filename, and operation.
Your current implementation of this (in your question, after editing) won't work. Using cin should however be fine. Place this earlier in your main(), before opening a file.
Use substr like you have, or alternatively, search for a space character. Assume that the input after this is your filename, and the input in the first section is your operation. Store these values.
After this, try to open your file. If the file opens successfully, continue. If it won't open, then complain to the user for a bad input, and go back to the beginning, and ask again.
Once you have your file successfully open, check which type of calculation you want to run. Counting a number of rows is fairly easy - you can go through the file one character at a time, and count the number that are equal to '\n', the line-end character. Some files might use carriage-returns, line-feeds, etc - these have different characters, but are both a) unlikely to be what you have and b) easily looked up!
A number of columns is more complicated, because your rows might not all have the same number of columns. If your input is 1 25 21 abs 3k, do you want the value to be 5? If so, you can count the number of space characters on the line and add one. If instead, you want a value of 14 (each character and each space), then just count the characters based on the number of times you call get() before reaching a '\n' character. The use of a vector as explained below to store these values might be of interest.
Having calculated these two values (or value and set of values), you can output based on the value of your 'operation' variable. For example,
if (storedOperationName == "numcols") {
cout<< "The number of values in each column is " << numColsVal << endl;
}
If you have a vector of column values, you could output all of them, using
for (int pos = 0; pos < numColsVal.size(); pos++) {
cout<< numColsVal[pos] << " ";
}
Following all of this, you can return a value from your main() of 0, or you can just end the program (C++ now considers no return value from main to a be a return of 0), or you can ask for another filename, and repeat until some other method is used to end the program.
Further details
std::get() with no arguments will return the next character of an ifstream, using the example code format
std::ifstream myFileStream;
myFileStream.open("myFileName.txt");
nextCharacter = myFileStream.get(); // You should, before this, implement a loop.
// A possible loop condition might include something like `while myFileStream.good()`
// See the linked page on std::get()
if (nextCharacter == '\n')
{ // You have a line break here }
You could use this type of structure, along with a pair of counters as described earlier, to count the number of characters on a line, and the number of lines before the EOF (end of file).
If you want to store the number of characters on a line, for each line, you could use
std::vector<int> charPerLine;
int numberOfCharactersOnThisLine = 0;
while (...)
{
numberOfCharactersOnThisLine = 0
// Other parts of the loop here, including a numberOfCharactersOnThisLine++; statement
if (endOfLineCondition)
{
charPerLine.push_back(numberOfCharactersOnThisLine); // This stores the value in the vector
}
}
You should #include <vector> and either specific std:: before, or use a using namespace std; statement near the top. People will advise against using namespaces like this, but it can be convenient (which is also a good reason to avoid it, sort of!)

C++: Getting character codes instead of letters when writing data to rich text box Text property

I am new to C++ and working on a relatively ambitious Win32 application in Visual Studio. The problem I'm having is that a text field in the main window is displaying a number instead of a letter when 'writing' from a string.
I'm reading a vector into a Rich Text Box, but instead of AA, the text box displays 6565. I understand that 65 is the character code for 'A', but haven't been able to find how to get the window to display the letter.
This is the code that creates the vector. Debugging this piece shows that the data loads properly. This snippet of code is part of a 'portfolio' class.
vector<string> tickers;
string cell;
string line;
ifstream d ("file.csv"); //The file contents look like: AA,AAPL,BAC and so on
if (d.is_open()) {
getline(d,line);
stringstream line2(line);
for (ci=0; ci<c; ci++) { //c is known and is the number of 'columns' in the file I am reading.
getline(line2,cell,',');
tickers.push_back(cell);
}
}
The rich text box's name is "Results2".
The code to set the Text property of the rich text box is:
portfolio p;
int n = p.tickers.size();
for (int i=0; i<n; i++) {
for (int j=0; j<p.tickers[i].size(); j++) {
Results2->Text += p.tickers[i][j];
}
Results2->Text += "\n";
}
I know that the "Results2->Text" bit is correct, because the data is getting there just fine. The problem is, I end up with:
6565
65658076
instead of:
AA
AAPL
What am I doing wrong?
Thanks in advance for any help!
It may be a Marshalling issue between your C++/STL string you are manipulating and C++/CLI String that the RichTextBox is taking.
EDIT
The problem is that you are putting the characters one by one in the RichTextBox::Text, or this field expect a System::String so it convert each character as an integer value to a String before putting it in the textbox, that's why you get the ASCII numbers instead of the string.
Since portfolio::tickers is a vector<std::string> as you specify in the comment, you could convert each std::string to a char* before giving it to the RichTextBox::Text :
portfolio p;
for (int i=0; i<p.tickers.size(); i++) {
Results2->Text += gcnew String(p.tickers[i].c_str());
Results2->Text += "\n";
}
You could also use the marshal_asintroduced in VC2008 :
Results2->Text += marshal_as<String^>(p.tickers[i].c_str());

Save/Load workspace in C++ application

I'm working on adding a new feature to an existing program. It's basically a save/load workspace feature, where a user can save the positions of their windows, and then load said positions whenever they want to by selecting a menu item. In order to implement this, I have created code which extracts the screen coordinates of the window and writes them to a file (below) :
void CMainFrame::SaveWorkspace()
{
RECT ctrlsize;
m_pDialog->GetWindowRect((LPRECT)&ctrlsize); //obtains location for window
ofstream Workspace("saveone", ios::out);
Workspace << ctrlsize.left << "," << ctrlsize.top << "," << ctrlsize.right << "," << ctrlsize.bottom;
}
And this (is supposed to) loads the workspace:
void CMainFrame::LoadWorkspace()
{
//Read in the data from the file
int data[3][4];
int r=0;
int a=0;
int b=0;
ifstream infile;
infile.open("saveone");
for(a = 0; a< 3; a++)
{
for(b = 0;b<4;b++)
{
infile >> data[a][b];
cout << data[a][b];
}
}
infile.close();
//Now, assign the extracted values
RECT ctrlset;
ctrlset.top = data[0][1];
ctrlset.left = data[0][0];
ctrlset.right = data[2][0];
ctrlset.bottom = data[0][3];
// Finally, reassign the window positions
m_pDialog->SetWindowPos(NULL, ctrlset.left, ctrlset.top, (ctrlset.right - ctrlset.left), (ctrlset.bottom - ctrlset.top), SWP_SHOWWINDOW);
}
Problems:
1) the SaveWorkspace function works sporadically; more often than not, it doesn't create a file.
2) the LoadWorkspace function doesn't work. Specifically, only the data[0][0] coordinate gets saved to the array (the first value in the file).
This seems like a fairly easy thing to do, I'm a bit embarrassed that it's giving me so much trouble...
EDIT: I've fixed problem #1. Now I just need to figure out why my array isn't working.
You have at least two problems in the reading.
Your array definition is wrong. It is :
data[2][3];
This has only 6 values.
However, in the loop you are reading 12 values out.
You have the "," values in the file. You are not getting rid of them.
Maybe as an easy solution, you could add a new line after each entry when you write them.
Or you could enter the details of a single rectangle on one line, then read the full line and parse for the individual components yourself.