-
reply
insert
Code:
//program that input two numbers and multiply it using addition
#include <iostream.h>
#include <conio.h>
void main();
{
clrscr();
int, x,y,prod;
cout<<"Input first number:";
cin>>x;
cout<<"Input second number:";
cin>>y:
for(x=0; x>=y; x+x)
cout<<"The product is"<<prod;
getch();
}
is there somethinG wrong in my code? IF YES, what part of code/program that does not satisfies this problem?
-
Ok, that's a good start. Especially the part where you read in the first two numbers.
Explain, in english how you want your for loop to work. A for loop has a start value, end value, and increment, and then it has a block of code that executes each time until the end value is reached. Explain what variable you want to use to loop through, the start value, the end value, and how it much it will increment each time. Then explain what will be done inside the block of code. Finally explain how that will solve the problem.
Your current for loop does not solve the problem, but if you explain in English what you are trying to do, we can find out if you are just having problems coding the solution, or if you are still trying to come up with the solution in the first place.
-
> is there somethinG wrong in my code? IF YES, what part of code/program that does not satisfies this problem?
Well it doesn't compile - did you try to compile it?
> void main();
a) main returns an int, see the FAQ
b) the ; at the end is really bad news for the rest of the code
When it compiles, run it and see what answers you get.
-
for(x=0; x>=y; x+x)
you ignore value entered by user replacing it by 0;
x+x does not modify the x value, so your loop or run never or forever based on the condition 0>=y
cout<<"The product is"<<prod;
prod is not initialized, you're going to print some garbage value
-
Code:
for ( int i=0; i<5; i++ )
{
std::cout<< i << '\n';
}
Declare and initialise all your variables first ... 0 is always a good number.
Read in the two numbers.
Change 5 in the above loop (used as an example) to one of the numbers the user inputs.
Inside the {}'s += the other inputted value to the product.
Don't need to cout<< inside the loop.
After it's finished print the value.
-
reply
it would not run properly!!!! the compilation result is an error!!!!!!!
-
Code:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int y,x,prod;
cout<<" INPUT 1ST NUMBER:";
cin>>x;
cout<<"INPUT SECOND NUMBER:";
cin>>y;
for( x=0; x<5; x++)
{
cout<<x<<'\n';
}
cout<<"THE PRODUCT IS "<<prod;
getch();
}
is there something wrong with this code?
-
yes it is. read the thread from the beginning
-
depends on the compiler you use.
my compiler says
Code:
error: `main' must return `int'
Kurt
-
Blacksnake, did you read the pointers I left in my last thread? Why does the for loop loop 5 times? Why do you cout<< instead of adding inside the loop's scope? Where is the product calculated?
Psudo code:
Code:
var Num1 = 0
var Num2 = 0
var Product = 0
get: Num1
get: Num2
loop ( 0:Num1)
{
Product += Num2;
}
print: "Product is: " Product
Change that to real C++ and it should work.
EDIT:
Also, "#include <iostream.h>" ... use: #include <iostream> instead. It won't fix the problems in your code. It's just iostream.h isn't really used much. You may have to put a 'using namespace std;' before main (or you could put std:: on front of cout<<and cin>>):
Code:
#include <iostream>
using namespace std;
int main( void )
{
cout<< "Hello World!";
return 0;
}
or
Code:
#include <iostream>
int main( void )
{
std::cout<< "Hello World!";
return 0;
}
-
reply
i'm using borlanD c++ compiler...
-
Are you compiling code?
Are you getting errors?
Are you trying to fix them?
Are you testing and trying things?
Have you tried to convert the pseudo code above to real C++ code?
We are not here to give you code or do your homework for you! We're here to help you along. I see very little evidence of your work here!
-
Are you compiling code? yes
Are you getting errors? yes...many times
Are you trying to fix them? yup...
Are you testing and trying things? yup...
Have you tried to convert the pseudo code above to real C++ code? no... i'm confused about that
-
Pseudo code is code which is readable. You read how it should work.
so, for example, my loop(0:Num1) could be the same as for ( int i=0; i<Num1; i++ ). Print: "whatever", would be like cout<< "whatever", get: Num1, cin>> Num1; and in this case var is int
Post your efforts and I'll be more than happy to help. use code tags though.
-
the code
Code:
// This program based on the old version of BORLAND C++
#include <iostream.h>
#include <conio.h>
void main()
{
int x;
int y;
int product;
cout<<"input first number:";
cin>>x;
cout<<"input second number:";
cin>>y;
for(int i=0; i<x; i++)
{
product +=y;
}
cout<<"Product is:"<<product;
getch();
}
the output should be:
input first number: 5
input second number: 3
Product is: 1549 (product should be 15)