Jump to content

How do i write the 'press r to repeat' code thing in C

Helomegussta
Go to solution Solved by minibois,

Work with functions and have the R button trigger the function, so it outputs what you want.

Just make most of your code into a function and have that function triggered once by the main and then have it trigger itself 

 

I don't know how to write C and I can't copy your code because it's a screenshot, so here is some pseudocode:

int main()
{
	RunVATCode();
}

void RunVATCode()
{
	// Insert the code above here
    // if the user presses R? run RunVATCode(); again
    // Else? terminate program.
}

 

There are of course dozens of other ways to achieve this, but this would IMO be the simplest to implement, just have a function that calls itself, or terminates the program.

 

 

so i wrote a code that calculates the VAT , but i want it to ask the user 'Press (R) to Repeat Or Any Other Key to Quit' but it doesn't work for some reason , am not that good at C yet so if anyone could help or explain in simple for ,what i need to do that would be great 

Capture.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

It appears that you don't have a label for repeat at all so you're goto statement doesn't actually jump anywhere. Unless I missed it somewhere in there.

 

It should look something like.

// snip
repeat:
printf(....);
scanf(....);
// snip

 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Work with functions and have the R button trigger the function, so it outputs what you want.

Just make most of your code into a function and have that function triggered once by the main and then have it trigger itself 

 

I don't know how to write C and I can't copy your code because it's a screenshot, so here is some pseudocode:

int main()
{
	RunVATCode();
}

void RunVATCode()
{
	// Insert the code above here
    // if the user presses R? run RunVATCode(); again
    // Else? terminate program.
}

 

There are of course dozens of other ways to achieve this, but this would IMO be the simplest to implement, just have a function that calls itself, or terminates the program.

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, minibois said:

Work with functions and have the R button trigger the function, so it outputs what you want.

Just make most of your code into a function and have that function triggered once by the main and then have it trigger itself 

 

I don't know how to write C and I can't copy your code because it's a screenshot, so here is some pseudocode:


int main()
{
	RunVATCode();
}

void RunVATCode()
{
	// Insert the code above here
    // if the user presses R? run RunVATCode(); again
    // Else? terminate program.
}

 

There are of course dozens of other ways to achieve this, but this would IMO be the simplest to implement, just have a function that calls itself, or terminates the program.

 

thank you 

Link to comment
Share on other sites

Link to post
Share on other sites

I would also add that goto shouldn't be used unless it greatly simplifies your code. goto is a trap for young players since it's very easy to abuse because it can do anything with zero regard for scope. This makes it easier to introduce bugs and makes it harder to fix them. Additionally, it makes code harder to follow in most cases since the code no longer follows any sort of logical progression since it can now jump anywhere. It's a big trap for young players.

 

In your case a while loop (or do while) or as @miniboishas suggested a recursive function might fit the bill as well. 

 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×