stuck from printing the correct output

the input: 1 5
the output:1 2 3 4 5 6
the input :10 10
the output:10 11 12 13 14 15 16 17 18 19 20

I been stuck doing this for 5 hours now and I really no clue where I get wrong. every time I input: 1 5
output : 0 1 2 3 4 5 6
input: 10 10
output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

I have tried all 3 different way but all of them can be either too high or error
I hope someone can point me the mistake I been making.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main()
{

int a, b, c;
int i ;

scanf("%d %d", &a, &b);

    for (i = 0; i<a+b; i++)
    {
    	if ( i >=10){
    		
    		c = a+ b;
    		printf("%d\n",c);getchar;
		}
		else{
		
    	
    	printf("%d\n", c);getchar;
        }   
	}
  
Last edited on
Small point: I believe you should use <cstdio> instead of <stdio.h>

And just to clarify because I am not sure. Is the program supposed to print the first number and then the second tells how many to print afterwards? Either way, have you tried stepping through it yourself, one by one? Take your 10 10 input. So you create a for loop that starts at zero and then goes up to ten plus ten. Therefore, the loop will run twenty times. Keep going through each step as if you were the computer.
Hello kenmariot,

The first question is this a C program or a C++ program?

If you save the file with a ".cpp" extension then Too Explosive makes a good point about using <cstdio>.

The variables "a" and "b" work better with a better name to describe what they are or do.

Inside the for loop I am not understanding what the if/else statement is for. In the else statement you are trying to print "c", but you never give it a value before you try to print it. Actually the variable "c" is not needed.

This can be done with one statement in the for loop.

Another point when you copied the code you may have missed the closing } of main.

I do not know what you like to work with, but when the program starts the user is staring at a blank screen having no idea what to input. You may know since you wrote the program, but others do not.

Give this a try and see what you think:

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
#include <cstdio>

//#pragma warning(disable : 4996)  // <--- Used to avoid a compiler error. You may not need this.

int main()
{

	int start{}, qty{};
	//int i;  // <--- Better defined in the for loop. Unless you need it outside the loop.

	printf("Start: ");  // <--- Prompt for the input. 
	scanf("%d", &start);

	printf("Qty: ");  // <--- Prompt for the input. 
	scanf("%d", &qty);
	printf("\n");

	for (int i = start; i < start + qty; i++)
	{
            printf("%d ", i);

            //getchar();  // <--- Is there a reason for this?
	}

	return 0; // <--- Not necessary, but makes a good break point.
}


The two test runs I did produced this:

Start: 1
Qty: 5

1 2 3 4 5

Start: 10
Qty: 10

10 11 12 13 14 15 16 17 18 19


I think this is what you are looking for.

The code for the prompt may be done better, but at the moment trying to put the prompt in the "scanf" was not working the way I thought it should. Then again it has been a long time since I worked in C.

BTW getchar is a function not a variable and should be written as getchar(). Although that is just one way of using "getchar".

Hope that helps,

Andy
@Handy Andy and @Too Explosive thank for the reply and I make a bit modify in my for and it works out good now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main()
{

int a, b, c;
 
 
    scanf("%d %d", &a, &b);
   

    for ( int i = a; i<=a+b; i++)
    {
			
    	    printf("%d\n", i);
    }
  
  }
.

1
2
3
4
5
6


as for the first question, i using C program language and just like you predict I have ".cpp" extension. And as for getchar I use it to avoid getting bufferflow (still learning what is bufferflow)so I just want to avoid put anything that might cause double print.

BTW I also discover my program run same output over and over again, is seem my compile get a bug or something it making me triple check-up by comparing my old and modify code. when I copy & paste to my new source file it works fine now. Also, can you tell what the difference between <cstdio> and <stdio.h> because I thought either is the same library?

The code can run at either library too.

P.S: I nearly thought how my program related to explosive until I saw the nickname...
Last edited on
Hello kenmariot,

When you use the ".cpp" extension the compiler will compile the program differently than if you used the ".c" extension.

The header files "stdio.h" and "cstdio" are in a sense the same as "cstdio" does include "stdio.h" at the beginning, but goes further to make the C header file work better with a C++ program.

I may not have this 100% correct, but the basic idea is sound.

So if you use the file extension is ".cpp" it is better to use the "c?????" header files even if you see no difference, there is a difference. Be aware that not every C header file has a C++ counterpart and it is not a good idea to mix C++ and C header files in a C++ program, but sometimes you have to and that is OK.

getchar is a function that should be written as getchar(). As the name implies it will get a single character from the input buffer. Not enough to deal with any possible buffer overflow. What you might consider using is scanf_s. I took a quick look at https://stackoverflow.com/questions/21434735/difference-between-scanf-and-scanf-s

I am not sure if scanf_s is specific to Microsoft compilers as the post suggests. Or if it became part of a C++ standard after C++98 or 99. I did not go that far to read up on the whole post, but the idea I get is that scanf_s is a more secure form that should avoid any buffer overflow.

Your use of getchar() in your program is just creating a pause waiting for the user to press enter to continue. But without a prompt before its use the user does not know what to do.

In your for loop you are using <=. This may work, but as your output shows you are printing six numbers when I believe you only want five numbers. The <= is causing one extra loop that prints the extra number. AS I showed you in my code the "<" is what you need to use here. Even if you start "i" at one it prints five numbers not six.

On line 5 you define three variables. First I state again that you should avoid using single letter variable names. It does not matter to me and the program is small an easy to see all of it on one screen, but in the future, using a variable that is a noun and describes what it is or does will benefit you in the long run and help others to understand your program better. So, the sooner you get use to creating better variable names the better off you will be.

Also the variable "c" is defined, but never used. This is not a problem with the compile, it should produce a warning, and it will not stop the program from running, but it is wasted storage space since it is never used. In a larger program it could make a difference holding space for a variable that is never used and allowing something to use that space.

You have a program and know what to enter and how to enter the information, but someone else has no idea what to do when presented with a blank screen and no idea what to do.

A lot of the time it is best to prompt the user with what to enter and an example of how to do it. Some times it is better to prompt for each input so that there is no question about what to enter.

I have seen times where I have had to create a whole new program because I could not figure out what the problem was. And even copying and pasting the same code worked. I have also had programs that I have had to comment out a line of code and rewrite the same code below the comment for it to work and other times where I had to copy a variable and paste it into a line of code for it to work. I have never figured out what caused the problem. Sometimes you just have to try several things or just give up and create a new program.

Hope that helps,

Andy
it really helps a lot @andy and I will keep the variable as the description to make others understand easier.
Registered users can post here. Sign in or register to post.