whats wrong with this program

This is a discussion on whats wrong with this program within the C++ Programming forums, part of the General Programming Boards category; #include <math.h> #include <iostream.h> #include <stdlib.h> int rounding( int y,int yaxis) { double a; char *buffer; int decimal,sign; a = ...

  1. #1
    papas
    Guest

    whats wrong with this program

    #include <math.h>
    #include <iostream.h>
    #include <stdlib.h>

    int rounding( int y,int yaxis)
    {
    double a;
    char *buffer;
    int decimal,sign;

    a = (y+10)/yaxis;
    buffer = _fcvt( a, 0 , &decimal , &sign );

    return buffer;

    };



    void main()
    {
    const screen_length = 79;
    const screen_height = 24;
    const minx = -10;
    const maxx = 10;
    const miny = -10;
    const maxy = 100;


    int column,row,x,y,yaxis,xaxis;
    double a,b;
    //float ;
    int temp;
    char h;

    a = abs(minx * (screen_length - 1) / (maxx - minx));//find the absolute value of a
    b = abs(maxy * (screen_height - 1) / (maxy - miny));//find the absolute value of b

    xaxis = (maxx - minx) / (screen_length - 1);
    yaxis = (maxy - miny) / (screen_height - 1);


    for (row=1;row<screen_height;row++)
    {
    x = minx;
    for(column=0; column < screen_length ;column++)
    {
    y = x * x;
    if(row == screen_height-rounding(y,yaxis))
    cout<<'*';
    else
    if(column == a)
    cout<<'|';
    else
    if(b == row-1)
    cout<<'_';
    else
    cout<<' ';
    x = x + xaxis;
    }
    cout<<endl;
    }
    }

    here is the equivalent working program in pascal
    the pascal program works
    damn im having serious probelms in converting it
    pls help

    PROGRAM GRAPHIC(INPUT,OUTPUT);

    VAR
    ROW,COL,A,B : INTEGER ;
    X,Y,YSCALE,XSCALE : REAL ;

    CONST
    SCREEN_LENGTH = 77 ;
    SCREEN_HEIGHT = 24 ;
    MINX = -10 ;
    MAXX = 10 ;
    MINY = -10 ;
    MAXY = 100 ;


    BEGIN
    A:=ROUND(ABS(MINX*(SCREEN_length-1)/(MAXX-MINX)));
    B:=ROUND(ABS(MAXY*(SCREEN_HEIGHT-1)/(MAXY-MINY)));
    XSCALE := (MAXX-MINX)/(SCREEN_LENGTH-1);
    YSCALE := (MAXY-MINY)/(SCREEN_HEIGHT-1);


    FOR row:=1 TO SCREEN_HEIGHT DO

    BEGIN
    X:=minx;


    FOR col := 0 TO SCREEN_LENGTH DO
    BEGIN
    Y:= X*X;
    IF (ROW = SCREEN_HEIGHT-ROUND((Y+10)/YSCALE)) THEN
    begin
    WRITE('*')

    end
    ELSE IF (COL = A)THEN
    begin

    WRITE(':');

    end
    ELSE IF (ROW-1=B) THEN
    begin
    WRITE ('_')
    end
    ELSE
    begin
    WRITE(' ');
    end;
    X:=X+XSCALE;

    END;


    WRITELN;

    END;
    READLN
    END.

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I can think of four probs right off the bat:

    1. You did not enclose your source in CODE tages
    2. You did not post your error messages
    3. You did not post the intent of the program
    4. You did not post your compiler


    Additionally, simplifying code to the smallest fragment that generates the problem is advisable. If this does not allow you to solve the problem on your own, it often makes help faster. iff you are asked for the context, or if the problem is not scalable, post the whole thing.

  3. #3
    asimos
    Guest
    well my mistake
    the program is supposed to generate a graph of the y=x^2(yes its me again )

    well the problem is that i cant get x which is a double to be equal with minx which is const and so if(row == screen_height-rounding(y,yaxis)) never applies this for beggining


    second dont know if the rounding function that i created works
    just found it in the msdn and said that it rounds numbers
    but gives me a warning when i try to compile that possible loss of data due to char* not able to convert to int

    im using microsoft c++ enterprise edition v 6

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    That is an odd rounding function.

    That is an odd program altogether.

    You might have better luck using a for block to increment y and then using gotoxy() to print the graph. That would get rid of a lot of useless iteration through cells that are not what you are looking for.

    As for that warning, that refers to the fact that a char is a smaller unit than an int. That is, less memory is used to hold a char than an int. Thus, a large int (bigger than 255) would suffer bitwise truncation if casted as a char. The implicit cast occurs when you return a char* from a function prototyped to return an int.

    That program is entirely too complicated for what you are trying to do. Try this:

    Code:
    #include <cstdio>
    #include <conio.h>
       using namespace std;
    
               
       int main () {
               
          clrscr();
          signed int x = -8;
          for (int xaxis = 2; xaxis<70; xaxis++) {
             gotoxy(xaxis, 12);
             printf("-");
          }
          for (int yaxis = 1; yaxis<24; yaxis++) {
             gotoxy(1, yaxis);
             printf("|");
          }
          for (x; x<9; x++) {
             gotoxy(x*x + 1, 12-x);
             printf("*");
          }
       
       }
    I switched it to a graph of Y squared because the console is about three times as wide as it is long, but you can change it back without a lot of trouble.

  5. #5
    asimos
    Guest
    thx triple v it worked
    thx alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 01:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 04:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21