Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

Need Help Writing C++ Programs. Please Read On:


  • Please log in to reply
22 replies to this topic

#16 KateetaK

KateetaK
  • Topic Starter

  •  Avatar image
  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:03:36 AM

Posted 22 September 2008 - 10:44 AM

Thank you. :thumbsup: Yeah, it does seem a bit difficult. The teacher isn't making it any easier either. She pretty much just tells you "Here it is." and doesn't explain WHY it is or how to do it ourselves. It's as if she's teaching herself something she already knows, if that makes any sense whatsoever.

OK, so I see what you mean about the cout << "\n";

Here's where I'm getting stuck (because I tried the same thing yesterday) - am I writing too many num's? Like how I have int num1, int num2...should I just have 1 single thing - maybe int num? And should I be getting rid of the endl at the end of those and replacing it with "\n"? If so, I suppose I would just use the one cout << endl; at the end. Because I tried to replace the the cout << num1 << endl; with cout << num1 << "\n"; and it didn't work any differently.

BC AdBot (Login to Remove)

 


#17 groovicus

groovicus

  •  Avatar image
  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:36 AM

Posted 22 September 2008 - 11:17 AM

There is no right or wrong way to program, well, when you are learning there is no wrong way. Your goal is to simply create a program that does what it is supposed to do, so that is all you should worry about.

What would happen if you just added cout >> "\n" after each line that you used to print out an integer? For instance, my program might look something like this:
int n=1;
int nn=2;

cout >> n;
cout >> "\n";
cout >> nn


#18 KateetaK

KateetaK
  • Topic Starter

  •  Avatar image
  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:03:36 AM

Posted 22 September 2008 - 01:49 PM

I went to her class today, she told me that I didn't even need the \n. She is ridiculous. She wouldn't even help me understand it, she shrugged off everyones questions yet that was all we did in class. :|

So, here is what I ended up with. When I check the work, it's CLOSE but definitely not right. Unfortunately, she wouldn't help me find out what I did wrong (she just said something not being right with my semicolon, but didn't specify which one). The assignment was already due, so it's too late. The thing is, I really want to understand! Do you think you could you check it out for me and give me a hint to what I did wrong here?:


#include <iostream>

using namespace std;

int main()
{
int num1;


cout << "Enter a four-digit positive integer: ";
cin >> num1;
cout << num1 << endl;
cout << num1 / 1000 << endl;
num1 = num1 % 1000;
cout << num1 << endl;
cout << num1 / 100 << endl;
num1 = num1 % 100;
cout << num1 << endl;
cout << num1 / 10 << endl;
num1 = num1 % 10;
cout << num1 << endl;
cout << num1 / 1 << endl;
num1 = num1 % 1;
cout << endl;




system("pause");
return 0;
}

Edited by KateetaK, 22 September 2008 - 01:50 PM.


#19 groovicus

groovicus

  •  Avatar image
  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:36 AM

Posted 22 September 2008 - 03:31 PM

Just out of curiosity, how is the output different than what you think it should be?

As far as syntax errors, I don't see any. I am a tad busy at he moment, so I will look again later. Nothing pops out at me as blatantly wrong though.

EDIT: Silly me. I was just looking for errors, and I didn't pay any attention to what you were doing. You are making it way too hard.

As I said before, there is no single right way to do a program. All you are doing is problem solving, and pretty much any problem can be broken down into a series of steps that you can write down on paper. Once you have it down on paper, then you can write the program to solve it. Here is my solution, keeping in mind that there are much more simple ways of doing this, but at this point, you wouldn't know any of those.

1) Get the input and store it in originalNum. (We will say the number is 1234)
2) Initialize num1=0;
3) Divide originalNumber by 1000, and store that result in num1. (num1=1)
4) Print out num1 which contains the result of our number divided by 1000. (you already had this part figured out) (prints out 1)
5) Subtract from my original number to get rid of the thousands place and store that in originalNum.(We want to be left with 234)
6) Divide my new originalNum by 100 to get the hundreds digit, and store that result in num1 (num1 now holds 2)
7) Print out num1.
8) Subtract from my originalNum to get rid of the hundreds place and store that in origianlNum. (We want to be left with 34)
9) Divide originalNum by 10 to get the tens digit, and store that in num1. (num1 now holds 3)
10) Print out num1.
11) Subtract from originalNum to get rid of the tens place and store that in num1. (num1 now holds 4)
12) Print out num1.

***************

In reality, you can combine a few of those steps, but that is the basic process.

So going through the steps one by one. You should be pretty clear on steps 1,2, and 3.
int num1;
	int originalNumber;

	cout << "Enter a four-digit positive integer: ";
	cin >> originalNumber;

	   num1 = originalNumber/1000;
	cout << num1 << endl;

Step 5 might seem hard, but you need to remember that you already have the number in the thousands position stored in num1, so all you need to do to get rid of the thousands place is the following:
originalNumber=originalNumber -(num1*1000);

Steps 6 is a repeat of step 4, except you use 100 instead of 1000; same for step 8. Step 9 repeats step 6, except using 10 instead of 100, etc.

Draw it out on paper, and keep track of what each variable is holding before you do any coding at all.

#20 KateetaK

KateetaK
  • Topic Starter

  •  Avatar image
  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:03:36 AM

Posted 22 September 2008 - 04:28 PM

I'm getting this:

Enter a four-digit positive integer: 3214 (The number I entered)
3214
3
214
2
14
1
4
4

#21 groovicus

groovicus

  •  Avatar image
  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:36 AM

Posted 22 September 2008 - 04:35 PM

Read my post above. ^

#22 Romeo29

Romeo29

    Learning To Bleep


  •  Avatar image
  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:02:36 AM

Posted 22 September 2008 - 04:43 PM

If you a student then you should use endl for newlines.

cout << endl;

The "\n" is escape sequence which works for most languages but you should stick with C++ manipulators.

#23 Romeo29

Romeo29

    Learning To Bleep


  •  Avatar image
  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:02:36 AM

Posted 23 September 2008 - 03:54 AM

HINT:
A 4 digit number can be broken into sum of numbers multipled by powers of 10. E.g.,
3567 = 3000 + 500 + 60 + 7 = (3*1000) + (5*100) + (6*10) + (7*1)

Lets say ABCD is given number
ABCD = (A*1000) + (B*100) + (C*10) + (D*1)

We are working with int types, so fractions will be ignored in result.

How to get A?
A = ABCD/1000

How to get B?
B = (ABCD - A*1000) / 100

How to get C?
C = (ABCD - A*1000 - B*100) / 10

How to get D?
D = (ABCD - A*1000 - B*100 - C*10)

Easy? Now you can convert it into C++ in a way you like.

:thumbsup:

Edited by Romeo29, 23 September 2008 - 03:56 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users