4th dimension, time travel....they give me headaches!
4th dimension, time travel....they give me headaches!
The for loop runs four iterations, so count gets incremented by one four times. count goes from 0 to 1 to 2 to 3 to 4, then is displayed. The while condition is still true, so the for loop executes again: 5,6,7,8, display.
While condition still true, so for loop goes again: 9, 10, 11, 12, display.
Now the while condition is false and the outer loop ends.
As I interpret that code, only the last of the Fib numbers will be in the file.
You open it inside the loop, write a number, close the file.
The next pass through the loop will open the file, truncate the existing file (that is, delete previous content) and write one new number.
The file open and close actions should be outside the for loop.
Within the context given, the new character could be placed in at least a 2 element char array, with a null terminator, making it a string. Then concatenate that. That would also require that the target array be made a valid string at declaration.
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
char command[1024] = "";
char addition[2] = "";
char newchar;
cout << "Command Line Interface Test with Intellisense" << endl;
cout << endl;
newchar = _getch();
addition[0] = newchar;
strcat( command, addition );
}
It really helps if you tell us the error message you get, and what line it points to.
In your case, it looks to be a very simple fix - you need to include the string library if you want to use the string type.
#include <string>
And strictly speaking, you are not "returning" a string from your function. It returns an int, you're modifying the string parameter.
Need to add Alice to that list.
int wheels[3] = { 0 };
The { 0 } is the initializer list for the array. By setting the first element to 0, the compiler will intialize all the remaining elements to zero. That's just to ensure the array is at some known state before proceeding.
For scoring, you could take what you've got an continue in that vein, but I think it will get long and complicated before you're done.
But, in the pattern you have, you need to make good use of if...else if... structures.
if (labels[wheels[1]] == 1 && labels[wheels[2]] == 1 && labels[wheels[3]] == 1)
{
credits = credits - bet;
cout << "JACKPOT! You won 1,000 credits." << endl;
cout << "You currently have " << credits + 1000 << " credits." << endl;
}
else if (labels[wheels[1]] == 1 && labels[wheels[2]] == 1 ||
labels[wheels[1]] == 1 && labels[wheels[3]] == 1 ||
labels[wheels[2]] == 1 && labels[wheels[3]] == 1 )
{
credits = credits - bet;
cout << "You won 100 credits." << endl;
cout << "You currently have " << credits + 100 << " credits." << endl;
}
So, this tests first for the most restrictive possibility, then a lesser version.
From your question above, it seems you might be a bit weak in the area of dealing with arrays to represent concepts as opposed to simply storing collections of values.
If I was writing this program, I'd have a six element array of integers, use the values in the …
You should generally work on a program in stages - heroic programming (writing the whole thing at once and hoping for the best) seldom works. Get one thing working, then move on to the next building on that.
So, here's what may serve as a foundation for you.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void three ( );
int main(void)
{
srand( unsigned( time( NULL ) ) );
for( int i = 0; i < 5; i++ )
three( );
return 0;
}
void three()
{
const string labels[6] =
{
"STAR",
"BAR",
"SEVEN",
"PLUM",
"LEMON",
"CHERRY"
};
int wheels[3] = { 0 };
//spin the wheels
for( int i = 0; i < 3; i++ )
wheels[i] = rand( ) % 6; //no +1, using as indexes
// display wheels
for( int i=0; i < 3; i++ )
cout << labels[wheels[i]] << " ";
cout << endl << endl;
}
You should be able to modify this to handle any number of wheels, simply by passing in a paramter of how many wheels to use in any invocation.
You should be able to simply the scoring portion, again using loops to examine the contents of the wheels array.
I don't have time at this moment to dig into the whole thing, but the fact that you get the same result every time you run the program is because you are not seeding the randomizer. In main( ), add:srand( unsigned(time(NULL)));
Your generation of the random x value is outside the loop, so only one x is ever created, you keep reusing the same value.
Your loop statement at line 21 - do you mean to create an infinite loop? 1 will always equal 1. I think a clearer way to express that would be while( true)
Do you really mean along the x axis, and changing their y position some amount per second?
We don't solve problems here, we help you with your solution. Write some code, ask some questions.
You could create an array or vector that holds each of the 6 words. Then the random number you generate is used as an index into that array to display the appropriate word.
Also, using those three random numbers will allow you to shorten your comparison tests for winning combinations, at the expense of a bit of clarity.
Consider also using and enuneration of the words.
Just another point. This bit of code in the void three( )
for (int i = 0; i < 3; i++)
{
(j = (rand() % 6) + 1);
}
Isn't doing a whole lot of good. Three random numbers get generated, but only the last one is stored and available for further use. Maybe you need something more like:
w1 = (rand() % 6) + 1);
w2 = (rand() % 6) + 1);
w3 = (rand() % 6) + 1);
I don't think the .ignore( ) is needed after 36 or 40, only after the last extraction operation before the loop goes back to getline. So only at line 52.
From your code, I'm assuming the matrix will be filled completely, so ROWa is a valid limit on number of rows to search.
If the size you search for can be in any position in a row, the best you're going to be able to do is return the row's index in which it's found. If it is in a specific column of any row, that's all you'd need.
The simplest search goes something like (1D array example):
int sch( int d[], int limit, int tgt )
{
int i;
for( i = 0; i < limit; i++ )
if( d[i] == tgt )
return i;
return -1; //tgt not found, invalid index
}
use a loop like you already have, and use the same loop counter variable as the index for names and for scores.
Don't forget to put some space between the two in your output statement.
Also, if you intend to get firstname lastname in one string input, you'll need getline()
cin >> a_string;
will store just one word's worth of content, stopping at the space between name parts.
getline( cin, a_string );
will read everything till the newline.
In line 24, if you want to display a name for the student, you need to use the same index variable as the loop's counter.
As a beginner, the easiest thing is to just always putusing namespace std;
after your program's #include statements.
Like Nike says, "Just do it."
Where is it written? If you follow up the chain of library files, you'll come to:
yvals.h
Which has the line:#define _STD_BEGIN namespace std{
Most every library file, after its #include statements, leads off with:_STD_BEGIN
(at least that's where it exists in MS Visual C++)
First show some work. Then we'll help you with specific problems.
The two groups of functions you are aske to replicate are really pretty simple. Your biggest task is to actually instantiate the strings.
For the problem you have, your output function needs to be set up like your ShowData().
You'll have to decide how you want to write the data to the file. You could write all the names, then all the data, by rows.
Or you could write a name, then all its sales date, new line, name, sales....
Just be sure you set your reading function to properly align the data when read back.
Putting it in a readable form:
void foo(int i)
{
if(i>1)
{
foo(i/2);
foo(i/2);
}
cout<<"#"<<endl;
}
I don't see where a 7 results, regardless of input. This function doesn't really do anything other than waste time and memory.
What's your real question?
Tablets have their place for consumption of content, and lightweight (very lightweight) productivity use. But for really doing work, I don't see the desktop or mid-to upper end laptops going away. Tablets don't have the storage, the power, the ergonomics of usage that a full up machine has.
The content they consume has to be made somewhere.
even with the return statement moved to the end of the function, the linecout << arr1;
will not display the array contents. You need to walk through the array, displaying each element's value one at a time.
It is left as an exercise for the OP to explain what is actually displayed with his original output statement.
You should make an attempt to solve the problem first. Then, point out what's giving you difficulty, and we will try to help you find the solution.
For your problem, start by writing out the steps you would take to do this by hand. What input do you need, what decision do you need to make based on that input, what calculations do you do?
You don't need the while loop. Just decrememt postion as the loop does, then do the divide and modulus and ... voila.
Well, that works when the position given is actually within the number. Do you need to handle the case of user asking for 7th position of a 3 digit number?
Is an else loop nessecary?
First of all, else is not a loop - it's a one time action.
Yes, you should have an else that encloses your action, if you don't just exit the program at that point. So one of the two models below should be used.
f.open( "somefile.txt" )
if( !f )
{
cout << "Error opening file. Exiting now.";
return -1;
}
//otherwise, main body of program continues
// or
f.open( "somefile.txt" )
if( !f )
{
cout << "Error opening file. Skipping main work";
}
else
{
//otherwise, main body in the else block
}
//program only exits at its normal end point
You are processing all the inputs in the while loop.
Then you one time write a value to output. Only the last data from the input reading gets stored.
I'd restructure to open the output file right after you've opened and tested the input file. Then after each input line is processed in the loop, write its result, also inside the loop.
Additionally, you test for success of the file opening attempt, but in the event of error you still go on to do the reading and processings - of nothing! You should either exit the program in the error handling block, or put the whole proccessing section in an else block. You should also test for success of the output file opening attempt.
Implement a James Bond changing license plate.
In both the distance( ) functions above, you pass in a miles(driven) paramenter, then do an input action to that value. The general concept should be that the user of an instance of your car supplies that value to the car object, through that parameter, and the car just makes the change. Like this:
void distance (miles){
fuelLevel -= miles/mpg;
}
You might want to put a check in there that the fuel level doesn't go below 0.
I don't think the name of that function really makes sense in the context of the car. It's more like "consume_fuel".
Look at the relationship of your line 7 and line 9. How does index relate to the bounds of the array?
for (int index = 0 ; index < size2; index ++)
if (sorting_array[index] > sorting_array[index + 1])