help again please

Idk how to post code like other people are doing. Sry if that is making this difficult. But I had to write a program that you are prompted to pick a starting number and ending number, and pick and increment to count between them in. so I could say 0 to 100 by 10's for example.

idk if this code is correct and the compiler is just confusing me but I cant get it to prompt me to ask for the numbers but it does convey everything else properly. Is this correct, or can anyone see what I'm missing if its not?

#include <iostream>
#include <stdio.h>

/*************************/
/* Joshua M. Spalding */
/* CECS 130-50 */
/* Assignment 3 */
/*************************/

/*This program will ask you to select 3 integers, one to start at, one to end at, and the last will be the increments you count in */
int main() {
int x=0;
int start=0;
int increment=0;
int end=0;
printf("\n Enter the number you want to Start at...");
scanf("%d", &start);

printf("\n Enter the number you want to end at...");
scanf("%d",&end);

printf("\n Enter increment to count between you numbers in...");
scanf("%d", &increment);

for( int x = start; x > end; x += increment )
std::cout << x << '\n';
std::cout << "\n End!\n";

return 0;}
Last edited on
for( int x = start; x [???] end; x += increment )

I don't know if this code is correct

Unless you tell us the specific problem your program is having, it is hard to tell you how to fix everything.
my problem is it isn't prompting me to put in the values for start, end, and increment like its supposed to. but I'm wondering if its because my compiler is seeing info from an old program and I cant get it to clear out old info. basically I was wondering if it will work properly for anyone else. if you run it does it ask for the numbers and display them in a countdown/countup fashion? sry I'm bad at this stuff
its supposed to ask the questions and then say 0, 10, 20, 30, 40...End! depending on your input of course.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

/*************************/
/* Joshua M. Spalding */
/* CECS 130-50 */
/* Assignment 3 */
/*************************/

/*This program will ask you to select 3 integers, one to start at, one to end at, and the last will be the increments you count in */
int main() 
{
	int x=0;
	int start=0;
	int increment=0;
	int end=0;
	cout << "\n Enter the number you want to Start at... : ";
	cin >> start;

	cout << "\n Enter the number you want to end at... : ";
	cin >> end;

	cout << "\n Enter increment to count between you numbers in... : ";
	cin >> increment;

	for(x = start; x > end; x += increment ) // [???]
	cout << x << '\n';
	cout << "\n End!\n";

	return 0;
} 


So you use #include <iostream>, but you are writing code more like C code not C++ code. Using std::cin should be safer than scanf().
Spacemanspalds wrote:
Idk how to post code like other people are doing. Sry if that is making this difficult.

Normally you just select the code and then press the <> button. Unfortunately, there is a forum bug when starting a new topic that prevents you from using the buttons. You can surround the code with [code] and [/code] manually or just make the post and then press the edit button which should allow you to use the buttons.

Spacemanspalds wrote:
if you run it does it ask for the numbers and display them in a countdown/countup fashion?

It asks for the numbers but the loop does not run because the loop condition is wrong.

I'm not sure if scanf is guaranteed to flush stdout automatically. You might have to put fflush(stdout); between printf and scanf.
Last edited on
Topic archived. No new replies allowed.