C++ program

Writing a simple program

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 7840 Views - Last Post: 28 September 2010 - 04:37 PM Rate Topic: -----

#1 marshaj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 10

C++ program

Posted 27 September 2010 - 03:10 PM

Hello:
I am new to C++ and I need to write a simple program to print out consecutive numbers from m to n given m=4 n=10 .
i have tried various code and nothing seems to work

this a the code
Please help ,give me a hint what might be wrong....

include <stdio>
int main() {
	//prints numbers from m to n
	int m,n;
	int begin =4;
		int end=10;
	int m=4;
	int n=10;
	int count=0
		int sum=begin +1;
	
	
		while(count<=10){
			count +1=count;

			printf("%d\n",count);
	         
		
			}
	

}


Mod edit - Added :code: tags

Is This A Good Question/Topic? 0
  • +

Replies To: C++ program

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: C++ program

Posted 27 September 2010 - 03:15 PM

Hint- use a for loop.
for(int i = /*initial value*/; i <= /*end value*/; i++){
    //now print i
}


Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: C++ program

Posted 27 September 2010 - 03:18 PM

First off you have a lot more variables than are needed. All you should have to worry about is an m and n integer. Second, you redeclare m and n, that will cause problems. Third, you are trying to assign variables like so count+1=count;, that is invalid in C/C++, you have to do it like so: count=count+1; (though you could also do it like this: count += 1; or count++;

Now, given that you are learning I won't post code, I'll just give you some hints and let you try to come up with something that works from there.

I would create a loop starting at m and going until n that prints out whatever you use to keep track of your current number each time through (this may be m or another variable).

Hope that is of use.
Was This Post Helpful? 0
  • +
  • -

#4 marshaj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 10

Re: C++ program

Posted 27 September 2010 - 04:23 PM

Thanks
I tried a little different code and compiled the program
Actually I am trying to learn to use the while with this code
It didn't give me exactly what I wanted but I'm still working on it
this what i got so far
#include <stdio.h>
int main() {
	//prints numbers from m to n
		
	int m=4;
	int n=10;
	int count=0;
		
		while(count<=10){
			count +=1;
			printf("m:%d\n"n);
	         
		
			}
}


This post has been edited by macosxnerd101: 27 September 2010 - 04:35 PM
Reason for edit:: Added code tags. Please use them in the future.

Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: C++ program

Posted 27 September 2010 - 04:41 PM

You're over complicating this. All you need is one variable.

Btw, That code is C not C++

In C++
#include <iostream>

int main()
{
  int count = 4;
  while(count <= 10)
    std::cout << count << std::endl;

  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: C++ program

Posted 27 September 2010 - 04:45 PM

no need for count,macosxnerd101 gave you a big hint. ask yourself what is the "initial value" and what is the "end value" then look at his code, not sure if should have even given that to you. if you required to use a while loop then initialize an int before the loop and increment that int at the end of each loop.
Was This Post Helpful? 0
  • +
  • -

#7 marshaj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 10

Re: C++ program

Posted 27 September 2010 - 05:18 PM

View Posteker676, on 27 September 2010 - 03:41 PM, said:

You're over complicating this. All you need is one variable.

Btw, That code is C not C++

In C++
#include <iostream>

int main()
{
  int count = 4;
  while(count <= 10)
    std::cout << count << std::endl;

  return 0;
}


Thnaks much
I think your while loop is missing the open and closed curly brackets???
Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: C++ program

Posted 27 September 2010 - 05:21 PM

Quote

I think your while loop is missing the open and closed curly brackets???


Nope it isn't. Well, yes it is but the code will run correctly. When all you have is one line you can skip the brackets. It can be a cause of some confusion but when the code is indented correctly it's easy to see what's happening.

This post has been edited by eker676: 27 September 2010 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

#9 pkjm17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 31-July 10

Re: C++ program

Posted 27 September 2010 - 08:58 PM

#include <iostream>
using namespace std;

int main()
{
  int n = 10;

  for(int m = 4; m <= n; m++)
    cout<<m <<endl;

    cout<<endl;
    //system("pause");

   return 0;
}

Was This Post Helpful? 0
  • +
  • -

#10 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: C++ program

Posted 27 September 2010 - 09:37 PM

View Postpkjm17, on 27 September 2010 - 09:58 PM, said:

#include <iostream>
using namespace std;

int main()
{
  int n = 10;

  for(int m = 4; m <= n; m++)
    cout<<m <<endl;

    cout<<endl;
    //system("pause");

   return 0;
}

You shoudn't use
system("pause");
to pause the console instead use
 cin.get();
and sometimes that dosn't work if there is already input in the buffer then use
cin.ignore(); 
cin.get();


Hope i was of help.

This post has been edited by Alex6788: 28 September 2010 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 marshaj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 10

Re: C++ program

Posted 28 September 2010 - 08:26 AM

Alex688:
Thanks for your post but you have totally confused me on the system pause.
I just need to print numbers from m to n with those variables.
I need to keep it as simple as possible by using the while loop and the printf functions.

Mj
Was This Post Helpful? 0
  • +
  • -

#12 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: C++ program

Posted 28 September 2010 - 08:29 AM

View Postmarshaj, on 28 September 2010 - 09:26 AM, said:

Alex688:
Thanks for your post but you have totally confused me on the system pause.
I just need to print numbers from m to n with those variables.
I need to keep it as simple as possible by using the while loop and the printf functions.

Mj

This isn't about the program and cin.get(); is simple, i just saw system("pause"); in your code and whenever i see that i always tell them they should use cin.get(); because system("pause"); is bad for many reasons and shouldn't be used really and cin.get(); is so much better.

This post has been edited by Alex6788: 28 September 2010 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#13 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: C++ program

Posted 28 September 2010 - 09:19 AM

You shouldn't use system("pause"); because it's a call to another program (on your system) called "pause".

This affect your program performance and portability.
Was This Post Helpful? 0
  • +
  • -

#14 masoug  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 181
  • Joined: 30-December 09

Re: C++ program

Posted 28 September 2010 - 12:55 PM

system("pause"); is okay, but like anonymouscoder said, it calls another program, which is not recommended. If its just a simple program like this, then it really wouldn't matter too much. On the contrary, don't use this on a larger, more complicated program. Plus, if you are going to use system("pause"); then put it at the end;

#include <iostream>
using namespace std;

int main()
{
  int n = 10;

  for(int m = 4; m <= n; m++)
  {
    cout<<m <<endl;
  }
  system("pause");
  return 0;
}


PS. Might need some verification: Is the pause command Windows only?

-Masoug

This post has been edited by masoug: 28 September 2010 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#15 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: C++ program

Posted 28 September 2010 - 01:01 PM

View Postmasoug, on 28 September 2010 - 01:55 PM, said:

system("pause"); is okay, but like anonymouscoder said, it calls another program, which is not recommended. If its just a simple program like this, then it really wouldn't matter too much. On the contrary, don't use this on a larger, more complicated program. Plus, if you are going to use system("pause"); then put it at the end;

#include <iostream>
using namespace std;

int main()
{
  int n = 10;

  for(int m = 4; m <= n; m++)
  {
    cout<<m <<endl;
  }
  system("pause");
  return 0;
}


PS. Might need some verification: Is the pause command Windows only?

-Masoug

Yeah I'm pretty sure it's a windows command, One bad thing is that it prints "Press any key to continue" on the screen and you might not want that.

This post has been edited by Alex6788: 28 September 2010 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2