I'm making a program where a person can input a direction and in the if statement, it adds/subtracts x/y axis and it draws a line after it gets over. The problem is that for some reason, it only works at the first iteration and doesn't draw any more lines after that.

I added a cin >> x >> y to test it out but it only draws one line and doesn't draw anymore. Initially, the choices were in a switch statement but I changed to if because I thought that was causing the error.

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

char b = NULL;
char u = 'œ';
char d = 'd';

int main()
{
    cairo_surface_t *surface = cairo_image_surface_create_from_png("background.png");
    cairo_t *cr = cairo_create(surface);
    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_set_line_width(cr, 5);
    double x = 0, y = 240;
    cairo_move_to(cr, x, y);
    long int count = 0;
    int cl = 0;
    int crr = 0;
    int choice = 0;
    int n;
    system("cls");
    while (choice != 5)
    {
        cin >> x >> y;
        cairo_line_to(cr, x, y);
        cairo_stroke(cr);
        cairo_surface_write_to_png(surface, "spiral.png");
        cout << "Current no. of points are : " << count << "/4096" << endl;
        cout << "Enter direction: \n" << endl;
        cout << "1 - Top Left \t 2 - Up \t 3 - Top Right " << endl;
        cout << "4 - Left \t 5 - Stop \t 6 - Right" << endl;
        cout << "7 - Bot. Left \t 8 - Down \t 9 - Bot. Right" << endl << endl;
        cout << "Enter you choice: ";
        cin >> choice;
        if (choice == 1)
            cout << "Test";
        else
        {
//More choices include the direction the person needs to go and it subtracts/adds to the x/y part
            cout << "How many times ?: ";
            cin >> n;
            for (int i = 1; i <= n; i++)
            {
                                x++;
                count++;
                cl++;
                if (cl == 256)
                {
                    cl = 0;
                    crr++;
                }
            }
            system("cls");
        }
    }
}

I expect it to draw lines to a particular direction. Say the person inputs right, it draws a line towards right and so on. But here, no lines get drawn at all (except if I add a cin >> x >> y at the start of the while loop, that draws one line and that's it, no more lines.)

1 Answers

0
Uli Schlachter On Best Solutions

This fails because there is no current point anymore. After cairo_stroke(cr);, you can add cairo_move_to(cr, x, y); and it should start drawing more lines in the way you expect. I think... I'm not quite sure what you are up to with this program.