The lines ranging from 26 to 50 should be enclosed in a while loop that keeps running until the user guess = the random number.
gibson.nathan commented: great posts, very helpful +1
The lines ranging from 26 to 50 should be enclosed in a while loop that keeps running until the user guess = the random number.
No problem, glad to help :)
You just need to change your 2 inner while loops to if/else statements.
if( number % 2 == 0 )
on line 15
AND
else
on line 20
Good job, you're almost there.
How about instead of using E to exit you use an integer like -1. This way you don't have to mess around with strings and parsing integers and equals methods. Why make things harder than they need to be? ;)
Maybe I'm misinterpreting the problem, but don't you want the user's input to be a number? If so, change the userInput variable to an int, and use that as the test in the if statements. The scanner will use the function nextInt();
Good luck, you're getting there.
The data type on your variable seems wrong to me. The input should be an integer (i.e. the number grade). The way you do your scanner input would also change along with the data type.
Good luck.
You have most of the code written to solve your problem already. You just need to put it in the right spot.
Re-write your division case as follows:
case '/': answerd = Numbera / Numberb;
if (Numberb == 0 )
{
cout << "ERROR Cannot divide by 0" << endl;
break;
}
cout << answerd << endl;
break;
This way the answer will never be output if the second number is 0 and the operator is division.
Also, you don't need anything after the switch statement besides the system pause. Hope that helps.
To use code tags, press the code button when you want to post code. Dani really can't make it any easier than it is now. ;)
Hi again,
First of all, your code is really messy. You can't write functions inside of main. You should write your function after main concludes. Also your function needs to be enclosed in braces{ }. Finally, you need to provide a function prototype before main.
Your code should look something like:
#include<iostream>
using namespace std;
int sumTo( int x, int y); //function prototype
int main()
{
int a,b;
cout<<"please input two integers " <<endl;
cin >> a >> b;
int sum = sumTo(a,b); //call the function
cout<<sum;
return 0;
}//end main
int sumTo(int x, int y)
{
//write code for the function here
return sum;
}//end function
You are making progress. Keep it up and good luck.
Just use an if statement to test which number is larger.
if(x <= y)
{
for (int i = x; i<=y; i++)
{
sum+=i;
}
cout<<sum;
}
else
{
for (int i = y; i<=x; i++)
{
sum+=i;
}
cout<<sum;
}
Notice the difference in the two for loops. That should be of great help to you.
Please note that this is definitely not the most efficient way to test. But for such a small program, it will suffice.
Remember to declare your variables!!
You are very much on the right track. But have a few problems that are holding you back.
First, you can't use variables that haven't been declared with an appropriate data type. You've done that with high, low, and sum.
Low and high can be replaced by the x and y values respectively, as long as you ensure that the first number entered is smaller than the second. Also you need to declare sum and output its value outside of the loop.
Good luck.
Edit: You also want to include the high value when iterating through the for loop. So you'll need to use less than or equal.
In my opinion, it would be much easier to think of the each name as separate character arrays. Take each name in separately and print the 0th element of each.
#include <iostream>
#define MAX 20
using namespace std;
int main()
{
char first[MAX];
char last[MAX];
char mid[MAX];
cout<<"enter first name"<<endl;
cin>>first;
cout<<first[0];
return 0;
}
That should be able to get you on the right track.
EDIT: If you need to use the get and ignore functions, sorry to have wasted both of our time.
Maybe my eyes are decieving me, but it looks like you just need to add another curly brace at the end of your main method. It seems to me that the while loop started on line 22 never gets closed.
You need to use == when doing if statements. You are suppose to compare(==) not assign(=) in if statements.
EDIT:
A few seconds too late i suppose ;)
Many apologies to the OP, I missed the brackets for the check array in the if statement. Thanks to Dave for pointing out the mistakes.
I noticed a few problems in your edit. First of all, remove the semicolon from the if statement. That is undoubtedly throughing off your output. The only other change that seems to be necessary is to test if check is NOT EQUAL to "\n". The following code produced the correct output when I ran it.
int count = 0;
char check[81];
char temp[81];
while (fgets(temp, sizeof(temp), fptr) != NULL)
{
fscanf(fptr, "%s", &check);
if (check != "\n")
{
count++;
}
}
printf("%d\n", count);
My text file looked like this:
Panic Attack
Dream Theater
Progressive Rock
Great Band!
The output was 4.
Try rewriting the while loop like this:
while ( fgets ( temp, sizeof temp, fptr ) != NULL )
Increment the counter inside the while loop as you have correctly done. Then output the result after the loop.
This is how I have done it successfully in the past. I'm sure there are other ways to do it as well. This is just what I have been able to come up with during my time with C. Good luck!
I dont understand this question at all. Can you please rephrase? The code you supplied doesnt make any sense. 1 will never equal 2 or 2 or 2...;)
Function definitions must appear out of the main(). I like to put the function prototypes first. Then open main, call functions, and close main. Then write the function definitions after main. Line 11 is also a problem. You don't include a data type for a function call. Also remember that void functions have no return value. Your current code is very confusing to me due to the functions being declared in main. Also remember to keep your parameters consistent. The function prototype, call, and definition must all have the same number of parameters, and they must be of corresponding data types. Also, you don't set values for the variables you include as parameters in the function prototype. (line 5) Try to resolve these problems before you tackle the rest of the program.
The parameter in the function is different from the parameter in the function call. To be more specific, the call has an array as the parameter, while the definition has an int value that is not an array. They must be the same data type.
But if I may offer my personal take on the matter:
I would make the function a void function with no parameters. Your current function is also wrong because it is declared as int but does not return an int value. Void solves this problem because a void function does not return any values.
Here is what I had in mind.
#include <iostream>
using namespace std;
//Function prototype
void fillArray();
int main()
{
fillArray();
return 0;
} //End main fn
//****************************************************
//Function definition
void fillArray()
{
const int SIZE = 3;
int num[SIZE];
for (int i=0; i < SIZE; i++)
cin >> num[i];
for (int i=0; i < SIZE; i++)
cout << num[i];
} //End fillArray fn.
The function creates the array, fills it, and outputs its contents. Let me know if you need any clarification.
tkud, you should give mention to the author of the code snippet. In the future, it would probably be better to provide a link to the code snippet this way the author is given his or her much deserved credit.
Oh , remove the word float from the function calls.
I shoulda spotted that earlier.
Are you getting compile errors or is it just not doing what you want it to do? If you are getting compiler errors, post them here.
Oh, ok. Ty. Sadly I have no clue what a parameter is..I shall look it up though. Thanks again for the information.
A parameter is what goes in the parentheses after a function call. The function call and the function definition must contain the same amount of parameters and they must be of corresponding data types. That explanation may be a little unclear. Look it uo online for more info.
If lines 36 and 44 are supposed to be function calls, you need to put () after the function name. Please correct me if I am wrong, but I think thats the problem.
EDIT
You will need to put in a parameter as well. MAke sure it is the same data type as the one in the function definition.
Sorry I missed that at first glance/
The if statement on line 23 seems problematic to me. There is no integer greater than 999 AND less than 1000.
The array isnt being output because you dont include that line in your program. You have to print the array when your done filling it.
To use code tags click the code button above the box in which you type your reply.
1. 1/3 * 3 is equal to 1. Is that what your program is telling you?
2. Accept user input of numbers to represent x and y. Then apply them to the formulas.
double x, y, product, expression2;
product = 3*x;
expression2 = product + y;
Not sure if this is exactly what you are looking for. The original post was quite vague.
ok thanks a lot for all your help I get it now. In my class the teacher just lectures and we don't program at all that is for hw!!!!
Glad I can be of help.
If you have no further questions please mark the thread solved.
If you have more questions, ask. :)
Thanks.
Sorry about that lol. and sorry if this seems basic but how do i ask the user to input the number from 1-30 but it can only be a number from 1-5
The while loop makes the process repeat 5 times.
To ask the user to enter a number use:
cout<<"enter a number between 1 and 30 inclusive"<<endl;
To accept the input use
cin>>number;
Obviously number must be declared as an int value. This same variable will be used in the for loop, so make sure they are the same. You'll also want to use an if statement to make sure the number input is actually between 1 and 30.
Also include this line before int main()
using namespace std;
I'm not sure if that was in your original code.
Do you think you can show me a completed copy of the code source that would help me visualize it better.
LOL, nice try. "Help me visualize it better"--That's a new one. ;)
You need to follow exactly what I said in my previous post.
Start your while loop.
Ask for and accept user input.
Begin for loop to print the number of asterisks. The number of asterisks will be as many as the number entered by the user.
Close for loop
Increment x by 1 using x++
Close while loop
So in essence, the for loop will be enclosed in the while loop.
Thats it. Give it another shot, you'll get it.
Ok, your original post says you want to read in 5 numbers so you'll want to use this as the condition in the while loop:
int x=1;
while(x<6)
{
//prompt to enter number
//accept user input
Next you'll want to use a for loop to actually display the asterisks
for(int i=1; i<=num; i++)
{
cout<<'*';
}
cout<<endl;
Finally you want to increment the value of x and close the while loop.
Try to understand that and let me know if you have any questions.
**EDIT**
You will also need to include an error check to make sure the values are between 1 and 30. Use an if statement.
Show us what you did so far then we will be glad to make corrections and suggestions.
As a preview, I think you will want to use a for loop nested in a while loop. We'll get into the specifics when you post your code.
Test if the number is even or odd using the modulus operator.
If the number % 2 equals 0, then the number is even.
If the number % 2 equals 1, then the number is odd.
In your first for loop you should be printing the students in course 1. I'd change line 42 to
System.out.println("Num of student in course1:"+course1.getnumberOfStudents());
Then your second for loop would be the same only replacing 1's with 2's.
Also I am assuming that your methods are defined somewhere in a separate file. Am I right in assuming this?
You cant end a program with an empty for loop. Either fill it with something or get rid of it. The compiler is expecting your for loop to have a body to it but it currently does not.
Yes I run Dev c++ on Windows vista and windows xp
You should just download a free c++ compiler. I use Dev C++ and I am 100% satisfied with it. With Dev you can type up your program , save it as filename.cpp, press the F9 key to compile and run. It's that simple.
Im glad we could finally solve the problem. Thanks for your patience. You were an excellent poster and showed great effort. Please mark the thread solved if you have no further questions. Thanks once again.
The problem may be resulting from the fact that there are only 9 planets and the loop is iterating 10 times. That is, the array has 9 elements, but the for loop is counting 10 times. Try changing <=9 to just <9. Let me know howit goes.
Can you please post your code so we can see the problem in context. Thanks.
*Sorry BestJewSinceJC I didnt realize you already asked the same question until after I posted ;) *
Yes sometimes a little focus can go a long way. Please mark the thread solved if you have no further questions. Thanks.
It doesnt appear that you have sum declared anywhere, unless you included it in the part A of the code. Also, you are not telling the program to do anything after the loop takes place. Output the variable sum after the for loop to see if your program actually works. ;)
<snipped> Yes Mikeyp926 is correct. Sorry for that post.
Can you please tell me what you are trying to accomplish with this program?
If all you are trying to do is print the string then you can use this
#include <iostream>
using namespace std;
#include <iomanip>
#include <String>
int main ()
{
system("cls");
string Object;
cout << "Enter the object you wish to create: ";
cin >> Object;
cout << "Initializing \"" << Object << "\"" << endl;
cout << "Initailization Complete" << endl;
cout << "Compiling \"" << Object << "\"" << endl;
cout << "Compiling Complete" << endl;
cout << "Creating \"" << Object << "\"" << endl;
cout << "FATAL ERROR IN C DRIVE" << endl;
cout << "ABORTING CREATION OF \"" << Object << "\"" << endl;
cout << "Program succesfully aborted" << endl;
cout << "Hard drive wiped to protect computer" << endl;
cout << "Have a nice day." << endl;
char wait;
cin>>wait;
return 0;
}
The last part with cin>>wait stops the screen to let you read the output before the screen closes.
Disregard Phil++'s code. It won't work because he messed up the quotes.
@ Phil++
If your gonna give away free code, the least you could do is make sure it actually works.
This isn't the first time you disobeyed the rules by giving away code.
Well thats a step in the right direction. You need to create variabes that correspond to the parameters defined in the method definitions. Then pass those parameters in the method call.
Here is how a method call generally looks
name_of_method(parameters separated by commas);
Not every method needs parameters, but in your case, all of your methods do.
Follow my last post (#8). Here I told you how I would call methods. You must write the name of the method and pass in parameters.
Read this if you get stuck again.
http://java.sun.com/new2java/divelog/part1/page6.jsp
Create variables of appropriate data types and pass them as parameters in the functions. Print the value returned by the function call. I'd do this in the main method.
The error on line 33 is because you are trying to return a double value in a method thats declared as int.
I'm not a big expert in astronomy, so I can't really help you there, but obviously you will need to put something in the main method. Try calling the functions in the main method to see if they are working correctly.
I knew you would say that because that is number 2 in your list of important things. lol
LOL. Just trying to enforce the rules of the forum. ;)