Thread: product using adition

  1. #16
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    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?

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.

  6. #21
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Exclamation reply

    it would not run properly!!!! the compilation result is an error!!!!!!!

  7. #22
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Exclamation

    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?

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes it is. read the thread from the beginning
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    depends on the compiler you use.
    my compiler says
    Code:
    error: `main' must return `int'
    Kurt

  10. #25
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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;
    }
    Last edited by twomers; 01-05-2007 at 03:27 PM.

  11. #26
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Exclamation reply

    i'm using borlanD c++ compiler...

  12. #27
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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!

  13. #28
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy

    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

  14. #29
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.

  15. #30
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy 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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix product
    By Cotizo in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2008, 10:10 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Product Keys
    By codegirl in forum Tech Board
    Replies: 6
    Last Post: 05-20-2006, 02:55 PM
  4. Microsoft Product Activiation loopholes
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-08-2005, 05:20 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM