Taking the code that you have and changing it a little bit you can make it look like this:
int functionA(int a, int b)
{
return a + b;
}
int functionB(int a, int b, int c)
{
return c * x(a, b);
}
Taking the code that you have and changing it a little bit you can make it look like this:
int functionA(int a, int b)
{
return a + b;
}
int functionB(int a, int b, int c)
{
return c * x(a, b);
}
Well all you need to do is inside your for loop copy the line of output to the screen and change std::cout
to whatever ofstream object you create. Don't forget to add curly braces to your for loop since it will become a multiline for loop.
You are on the right path. Do you need the results shown to the screen and writtent to the file or do you just need then written to the file? If you just want to write the data to a file then you need to modify the code in the for loop that displays the data to the screen to instead write it to a file. To that you need and ofstream object. Then instead of using std::cout
you would use the ofstream object you created.
As a side not have you used any of the classes you have in this code in your class? Can you use the STL? The code you have is somewhat advanced and if I was teaching an intro class and someone handed in what you have I would think they just copied that from the internet somewhere.
So you came up with the above code to read a file but you dont know how to output the resaults to another file?
If you want to retain what was held in the vector then before you exit your program you must write that information to a file. Then you also have to change your code so that when the program loads it loads the file into memory. Files are the only way I know of to make data persistent between launches of a program.
That wasnt so hard to do.
What code do you have so far?
That's a great attitude to have. To have main()
return an int is in the standard for a reason. Some systems need that value returned from main()
for them to function correctly. Some compilers will not even compile your code because of it. Just because you use it and you haven't had a problem does not mean it will work everywhere.
char const*
should be const char*
. May I ask why do you want to hold a constant string?
Sounds like fun.
The fact that the standard forbids it. main()
only ever returns an int.
What do you need help with? What code do you have so far? If "help me please" is slang for "give me code" then you will find no one is going to "help" you.
@Tumlee I know there is one for strings but it does not have a way to control how many characters are received in. The getline for char*
does have that ability though.
You dont want to use magic numbers. You want to use the constants that are supplied.
Well what is the best score? Is it closest without going over? Is it just the closest guess? Are you trying to see how many attemps it takes to find the random number? Once you know exactly what you want then you can start writing code.
Change for_each(newLines.begin(), newLines.end(), [](std::string& s){ reverse(s.begin(), s.end()); });
to for_each(newLines.begin(), newLines.end(), [](std::string& s){ reverse(s.begin(), s.begin() + 10); });
to only do the first 10 characters. I assume newLines is of type std::vector<std::string>
.
Don't give tips because my mind burnt out on studying functions, just give the code directly as I only need a very simple program. Thank you.
You will find that most people here wont go for that. I will also take it from the code you posted that you are using turbo c++. Unless this is what you have to use for school I would seriously look for a different IDE/Compiler. If you search there are plenty of references for good free up to date compilers.
No you can not overload ::
. Why would you want to?
So what is the problem you are having? No one is going to do this for you.
A reference is a constant pointer in the sense that the reference cant be changed to point to something else. You can still change the value that reference points to. On the other hand if you pass a const reference then you cant change the value that the reference holds.
Did you write this code? Besides the fact that there is no formating and it is mostly not standard compliant what is the issue?
So what have you tried so far?
Nice little tutorial.
you need to put lines 35 and 36 inside the do while loop. Also dont use void main()
. In the c++ standard it says that main returns an int.
Where should the cursor move to? Can you show us how the input should be formated?
We do not do your homework for you. What have you done so far? If you are having a problem with something that is one thing. To ask for us to do it for you is completly different.
if (!(cin >> num))
means if cin tries to get a number and fails then enter the if condition.
cin.clear()
get rid of the error flags in cin and allows you to use the stream again.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
means throw out all characters in the in buffer un till you either a newline character or you read in the maximum characters that the buffer can hold.
All the code does is see if you got something that cin wasn’t expecting. When you call cin >> num
cin knows that you are trying to read in an integer. When you supply a character the read fails since it is expecting a number. After the read fails cin's error state flag gets set to true. While the flag is true you will not be able to read any more information out of the stream. So we call cin.clear()
to reset the error flag to false. After we do that we want to make sure that the buffer is empty hence the call to cin.ignore()
.
As far as an easier way to check for valid input there might be but nothing is coming to mind at the moment. You might want to take all of your data in as a string and process the string but I think that would be more complicated than what i have presented to you.
This is a classic case of stream corruption. You asked for a number and got a letter so after that all stream operations will fail. one way to stop this is to use a loop when asking for inputs. here is an example:
int num;
do
{
cout << "Enter a number: ";
if (!(cin >> num))
{
num = 0;
cout << "Invalid input. Please try again\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (num = 0)
Yes. If intrest and the term are class members then you can pass them to or use them directly in the monthly payment function.
Well the intrest rate and term should be members of the class so you can use them in mutiple functions.
Well the first couple of things that I see right off the bat are
You didn’t write your main function like you are instructed to.
You pass the filename to your functions but never use it and instead you hard coded the file in each function.
You have three separate analyze functions when you should only have one.
Did you notice that all three of your functions are exactly the same except for the file name? The reason the function prototype your instructor gave you has it taking in the filename is that the one function should be able to handle any number of files as long as the files are formatted correctly. To use the file name from the functions parameters all you need to do is this
void analyzeData(string fname)
{
ifstream infile(fname.c_str());
// do stuff
}
You need to do the work. We will not do it for you. What code have you written so far?
So why do you want characters?
Well the problem you are having is you are not doing what the assignment asks. When you add to the letter if you exceed the bounds of the character set you need to wrap around to the beginning. You also don't want to modify anything that isn't a character. All you are doing is adding the rotation value to the letter. As a hint I'll give you a way to add to a letter and if past 'z' or 'Z' it will start over at 'a' or 'A' respectivly.
int rot = 4;
char ch = 'W';
if (ch >= 'a' && ch <= 'z')
ch = (((ch + rot) < 'z') ? ch + rot : 'a' + ((ch + rot) - 'z' - 1));
else
ch = (((ch + rot) < 'Z') ? ch + rot : 'A' + ((ch + rot) - 'Z' - 1));
So what do you need? Can you assume what a college student is and what you need to have for members and functions?
Why are you looking for spaces in a csv file? csv stands for comma seperated values. Your file should look like
1.1,1.2,1.3
2.1,2.2,2.3
...
Well if the map is set up like this:
map<"user-id", map<"movie-id", "rating">
Then if you want to see how many movies a person has rated you could do this:
std::map<int, map<int, int> > m;
// add stuff to m
int numberOfMoviesRated = m["some-user-id"].size();
In the above example the call to size calls the size function of the map for that particular user id. If you want to see all of the movies and ratings then you could do this:
std::map<int, map<int, int> > m;
// add stuff to m
for (auto it = m["some-user-id"].begin(); it != m["some-user-id"].end(); ++it)
{
std::cout << "movie id: " << it->first << "\trating: " << it->second << std::endl;
}
@ rubberman
I need to know how to remove spaces from a string using pointers in C++
Did you check to see if prices had anything in it? What happenes if you put this after line 17 and before line 18?
if(prices.size() < 2)
cout << "Vector size less than 2!" << endl
else
cout << "Vector has 2 or more elements!" << endl
A should actually look like this
if (x == 0) then //constant (1)
for i = 1 to n do // linear(n)
a[i] = i; //constant (1)
Resenje:
T(n) = O(1) + O(n) * O(1) = O(n)
B should look like this
i = 1; //constant (1)
repeat
a[i] = b[i] + c[i]; //constant (1)
i = i +1; //constant (1)
until (i == n); // linear(n)
T(n) = O(1) + O(1) * O(n) = O(n)
Yes what you came up with is correct. If you want to write it in big-O notation it would be O(n).
So what exactly are you haveing a problem with? What have you tried?
I would think they are just looking for big O notation since they give no size for n.
It means run forever since 1 will always be true. BTW you should start your own discussion thread to ask a question about something different.
you need to move the while loop outside of your for loop
void Concatenation( char dest[MAX_S_ROWS][MAX_S_COLS] ) // max_s_row = 208
{ // max_s_cols = 25;
for(int i=0, j = 0; i<200; i+=2, ++j)
{
myStrCat2(dest[j], dest[i],dest[i+1]);
}
while( j < MAX_S_ROWS ) // this loop is making the program close
{
dest[j++][0] = 0;
}
}
You could write your display like this
for (int i = 0; destination[i][0] != '\0'; i++)
cout << destination[i] << " ";
Your call to srand should be outside your loop. I would suggest putting it on line 15. As for you call to rand() it should look like this: r_number = rand() % 12 + 1;
. Your if condition on line 55 is also incorrect. Can a number be less than 1 and greater then 12 at the same time?
Thank you very much Mike
I was reading some articles on cplusplus.com and for kicks I read the template tutorial. At the very end of the section there is this paragraph:
Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.
Does this mean that if I have a .h file with a templated class in it I don't need to have inclusion guards on it? I normally just put them in out of habbit and it is not going to change that. It just made me curious if this is something that compilers are allowed to do or if they have to do it, ie. is it standard? IMO inclusion guards should always be used so I don't know why they would want to allow you to get used to not doing it.