I appreciate the replies guys and the patience with me lol. I had half expected a flurry of noob comments and 'do a search' etc..
@BranFlakes:
Youre right I meant bytes. I was thinking an increment of 1 in the memory location meant one bit but clearly that cant be right because if an int were 4 bits the most it could hold would be 0-16.. and thats certainly not true lol.
I had set sz to 10 for my debugging, so thats why I had that in mind; I just copied the fresh example code for this thread and forgot about sz being 100 in the original.
I guess Ill skip over 18 and work on the last few questions then, aside from the makefile ones.. I dont think with a modern compiler (or non commandline that is) that Ill need to deal with those kind of things.. at least for any of my purposes for a long time.
I do have another question actually, perhaps it should be in another thread but Ill ask it here anyway. the Cin function. I wrote a program which takes numbers from 1 to 1000 and a user input of the number of factors they wish to find. eg user puts in 7, the program lists every number from 1-1000 with a total of 7 factors, and outputs the numers and their factors. Then at the end I have a user option via a cin "y or n" to output the results to a text file.
I am using cin with the overload operator << (not that I really truly understand the whole overload concept yet), and I noticed if I input a character instead of a integer at first the whole program went haywire. So I had to add in some clauses, a cin.clear() and another cin argument which I dont entirely understand.
In anycase if I type 1y2y3y4y5y6y for example, it runs through and finds all the numbers with 1 2 3 4 5 6 factors, and outputs them all to the text file. I guess its kind of handy if this is the type of action you want to happen but its NOT what I want to happen. It seems cin keeps the other inputs in some sort of a buffer, and then automatically retreves them when its called to find such a character, so the first int cin input takes the 1, and the next being a char for the y or n, takes the y, the program loops and it automatically takes the 2 and so on.. is there a reason for this or a way to stop this? I tried adding a cin.clear() after each cin<<int/char but it does nothing, so Im guessing this function doesnt really 'clear' the input as I suspected it would.. my code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
//Finds numbers between 1 and 1000 with a given number of factors
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
inline string toStr(int a){
return static_cast<ostringstream*>(&(ostringstream()<<a))->str();
}
int main() {
while(true){
string outStr;
int factors;
int totalNums = 0;
cout<<"Find numbers between 1 and 1000 with how many factors?"<<endl;
cin>>factors;
cout<<endl;
//check for invalid entry
if(!cin){
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout<<"That's not a number! :(\n\n";
continue;
}
outStr = "Numbers with a total of " + toStr(factors) + " factors:\n\n";
//begin iterating through numbers
for(int i = 1; i < 1000; i++){
int numEvenDivs = 0;
//iterate through possible factors
for(int j = 1; j <= i; j++){
if(i%j == 0){
//if there are more factors than we're looking for, break out of loop
if(++numEvenDivs > factors)
break;
}
}
//display number and its factors
if(numEvenDivs == factors){
++totalNums;
outStr += "> " + toStr(i) + ": ";
for(int k = 1; k <= i; k++){
if(i%k == 0){
outStr += toStr(k)+ " ";
}
}
outStr += "\n";
}
}
//if there arent any numbers at all
if(totalNums == 0){
cout<<"No such numbers exist!\n\n";
continue;
}
//final output to console
cout<<outStr<<endl;
cout<<"A total of "<<totalNums<<" numbers exist with "<<factors<<" factors\n\n";
//check to see if user wants to output to a text file
cout<<"Would you like to output the results to a text file, type Y or N"<<endl;
char getResults;
cin>>getResults;
if(getResults == 'Y' || getResults == 'y'){
ofstream out(string(toStr(factors) + "-factors.txt").c_str());
out<<outStr;
cout<<"Results output!\n\n";
} else {
cout<<"\n";
}
}
}
|
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
:: this is the second command after cin.clear I was referring to. It refers to a standard object numeric_limits with an implicit cast to standard type streamsize.. right? anything in a <> is a cast correct? and then the ::max() I am totally lost, is that part of numeric limits? making it a function within a class within a class? Either way, without this code, upon character entry it still went psycho..
Also
static_cast<ostringstream*>(&(ostringstream()<<a))->str()
turns an integer into a string using the stringstream, namely the ostringsteam function.. So, what I get here is that, the integer a is overloaded into the ostringstream function, and taking the address of that conversion the str() function within ostringstream object is called, and because we're dealing with addresses at this point its cast to a pointer? I dont think I have this right. The multi line code was so easy and I do understand that, create a stringstream variable, take that variable and overload with your number, then take the ss variable, and call the .str() function and voila you have your string. I guess where I get lost is the whole ostringstream() and the address and pointer bits..
Anyway I guess Im going to finish up questions 25-28 which I should handedly do.. and then move onto chapter four. I don't really like moving past things until I truly understand them, and my above code even kind of bothers me for some of the things I used.. I guess, well, hope, most of my questions might be answered as I delve further into the book.
Thanks again :)