8 Replies - 747 Views - Last Post: 12 September 2012 - 06:13 AM Rate Topic: -----

#1 izic  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 197
  • Joined: 15-January 09

Printing consecutive numbers

Posted 11 September 2012 - 05:48 PM

I'm trying to print the consecutive numbers between two numbers in a cout statement. How would I go about doing this?

cout<<num<<" is the first number, the next "<<consecnum<<" consecutive numbers are "<<num2[1]<<endl;



Full code
#include <iostream>
using namespace std;

int main()
{
    int num, consecnum, sum, i, num3, j;
    
    cout<<"Enter a starting number"<<endl;
    cin>>num;
    
    cout<<"Enter a consecutive number."<<endl;
    cin>>consecnum;
    
    int num2[consecnum]; 
    
    
    sum = num + consecnum; 
    num2[0] = num;
    num3 = num; 
    
    int oddarray[j]; 
    int x=1; 
    int y; 
    
    for(i=1; num3<sum; i++) 
    {
         num2[i]=num+i;
         cout<<num2[i]<<endl;
         num3++;
    
    if(num2[i] % 2 != 0)
    {
           oddarray[x] = num2[i];   
           x++;
    }
    
}
    cout<<" "<<endl;
    cout<<num<<" is the first number, the next "<<consecnum<<" consecutive numbers are "<<num2[1]<<endl;
    
    for(y=1; y<consecnum; y++) 
    {
           cout<<oddarray[y]<<endl;   
           
    }
    
    
    system("pause");
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Printing consecutive numbers

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Printing consecutive numbers

Posted 11 September 2012 - 06:33 PM

Your code doesn't really resemble the description - so one is off.

Quote

I'm trying to print the consecutive numbers between two numbers


If you have a start and end number like your discription says (lets say 10 and 35) there is no need to add or make an array or any of that. Its just a loop from x to y
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2037
  • View blog
  • Posts: 6,060
  • Joined: 05-May 12

Re: Printing consecutive numbers

Posted 11 September 2012 - 06:36 PM

Back in 2011, you asked about for loops: http://www.dreaminco...oop-evaluation/

I think you need to review for loops some more.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Printing consecutive numbers

Posted 11 September 2012 - 06:41 PM

The OP has loops... ITs the rest of the logic that has nothing to do with the homework instructions.

WTF does this have to do with displaying consecutive numbers between x & y?

31 if(num2[i] % 2 != 0)

I suspect the OP just grabbed some code from some other assignment and pasted it in trying to meet the "give it your best effort" requirement of the site - expecting someone else to 'show' him the right way, and thus do the homework for him.
Was This Post Helpful? 0
  • +
  • -

#5 izic  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 197
  • Joined: 15-January 09

Re: Printing consecutive numbers

Posted 11 September 2012 - 10:43 PM

View PosttlhIn`toq, on 11 September 2012 - 07:41 PM, said:

The OP has loops... ITs the rest of the logic that has nothing to do with the homework instructions.

WTF does this have to do with displaying consecutive numbers between x & y?

31 if(num2[i] % 2 != 0)

I suspect the OP just grabbed some code from some other assignment and pasted it in trying to meet the "give it your best effort" requirement of the site - expecting someone else to 'show' him the right way, and thus do the homework for him.


I didn't grab anyone's code, I did this all my own. Also, one of the other program requirements was to add up the odd integers between the starting number and the ending number, that's why that part of code is in my program. If you don't want to help me fine, but please don't accuse me without knowing all the facts because you make yourself look bad.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Printing consecutive numbers

Posted 11 September 2012 - 10:50 PM

I never said you grabbed code from someone ELSE.
I said you grabbged code form some other project.
Considering it was from another part of this project, one that none of us knew were part of this, then it was a fair guess.

If you can't describe what you are really doing then how the heck are we to have any idea of just how far off you are?

You say you can't figure out how to print from x to y.
Its a loop from x to y. You have loops.

Without supplying a bunch of code that has nothing to do with the part you are having trouble on, please show us the block that IS causing you trouble and a description of the actual problem and the wrong output you are getting.
Was This Post Helpful? 0
  • +
  • -

#7 izic  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 197
  • Joined: 15-January 09

Re: Printing consecutive numbers

Posted 11 September 2012 - 11:07 PM

View PosttlhIn`toq, on 11 September 2012 - 11:50 PM, said:

I never said you grabbed code from someone ELSE.
I said you grabbged code form some other project.
Considering it was from another part of this project, one that none of us knew were part of this, then it was a fair guess.

If you can't describe what you are really doing then how the heck are we to have any idea of just how far off you are?

You say you can't figure out how to print from x to y.
Its a loop from x to y. You have loops.

Without supplying a bunch of code that has nothing to do with the part you are having trouble on, please show us the block that IS causing you trouble and a description of the actual problem and the wrong output you are getting.


I didn't come here to argue semantics alright? You said I grabbed code from another assignment which implies that I didn't do my own work which is not the case. My original post has two blocks of code - the full code and one line of code that if you read correctly would understand that that's the line of code I was trying to get my consecutive numbers to print out in. I didn't come here asking you guys to help find me the consecutive or odd numbers. I did of all this on my own, all I simply asked, and maybe I didn't correctly state it but I wanted my cout statement to print all consecutive numbers in one line.

Line from my original code:
cout<<num<<" is the first number, the next "<<consecnum<<" consecutive numbers are "<<num2[1]<<endl;



So for example if i entered 80 for the first number and 3 for consecutive number, the output would look like this:
80 is the first number, the next 3 consecutive numbers are 81, 82, and 83.

Once again, all I wanted to know was how to get the consecutive numbers to print out the I way I listed it above. That's it.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2037
  • View blog
  • Posts: 6,060
  • Joined: 05-May 12

Re: Printing consecutive numbers

Posted 11 September 2012 - 11:30 PM

Quote

I did of all this on my own, all I simply asked, and maybe I didn't correctly state it but I wanted my cout statement to print all consecutive numbers in one line.


You cannot do it with a single putting the output to cout unless:
- you call a function to return a string with the consecutive numbers; or
- prepare a string with the consecutive numbers before hand; or
- do some operator overloading hacks with the output stream operator.

In your original post, you wrote:

Quote

I'm trying to print the consecutive numbers between two numbers in a cout statement. How would I go about doing this?


but now in post #7 you write:

Quote

So for example if i entered 80 for the first number and 3 for consecutive number, the output would look like this:
80 is the first number, the next 3 consecutive numbers are 81, 82, and 83.


Post #7 conflicts with Post #1. In #1, you wanted all the consecutive numbers between two numbers. In #7, your are now saying that you want the next set of consecutive numbers.

I know you said that you didn't want to argue semantics, but in this case semantics matters. Which kind of behavior did you want: "between" or "next"?
Was This Post Helpful? 2
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon

Reputation: 3110
  • View blog
  • Posts: 9,482
  • Joined: 25-December 09

Re: Printing consecutive numbers

Posted 12 September 2012 - 06:13 AM

You need to show some actual effort at solving your problem. Show us what you tried to solve the problem of printing consecutive numbers. Nothing in the code you have provided so far shows this effort.

Also the following snippet:

    intconsecnum;
    
    cout<<"Enter a consecutive number."<<endl;
    cin>>consecnum;
    
    int num2[consecnum]; 


The C++ standard requires that arrays be declared with compile constant sizes. You are not allowed to use Variable Length Arrays in C++, according to the standard. So the last line is incorrect. If you want this functionality you should use a vector instead of the array.

Next if you are just trying to print consecutive numbers why do you even need an array or vector???

Your next step is to show us what you have done to try to solve your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1