GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Sep-2003, 00:09
Mvpsandy Mvpsandy is offline
New Member
 
Join Date: Sep 2003
Posts: 5
Mvpsandy is an unknown quantity at this point

Help! I Cant Get My Program To Work :(


hi i am a newbie to C++, and i am having problems with the program below

My goal: To make a program that will allow a user to enter a 5 digit number, and space that number by 3

so it will look like this

1 2 3 4 5



my code ...

CPP / C++ / C Code:
#include <iostream> 
 
using std::cout; 
using std::cin; 
using std::end1; 
 
int main () 
 
{ 
 
int num1; 
int num2; 
int num3; 
int num4; 
int num5; 
 
std:: cout <<"enter a 5-digit number\n"; //prompt 
std::cin >> num1; 
 
 
num1 / 10000; 
num2 / 1000; 
num3 / 100; 
num4 / 10; 
num 5 / 1; 
  
return 0; 
 
} 

what am i doing wrong
  #2  
Old 16-Sep-2003, 02:43
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Lemme get this straight, you want to place spaces between each digit in the number? So you input 12345, and it outputs "1 2 3 4 5"?

If so, you've not thought out your arithmetic straight. You're getting the input correctly, so everythings fine until line 20 (from the boards).

But look further down. You've got major problems here. num2 - num5 are all zero - nothing's happened to them. Then you're trying to divide zero by 1000 - still zero.

What you want to do is extract the digits from the number. OK, so you've got 54321. How to get the 5. You divide by 10000, but what happens to the 4321 remainder?

Use the mod operator %, which gives you the remainder!

54321 % 10000 = 4321
54321- 4321 = 50000
50000/10000 = 5

I'll do another loop:

(4321 - (4321 % 1000) ) / 1000 = 4

Just continue this, and you can then cout the answer nicely spaced.

GF
  #3  
Old 16-Sep-2003, 10:22
Mvpsandy Mvpsandy is offline
New Member
 
Join Date: Sep 2003
Posts: 5
Mvpsandy is an unknown quantity at this point
Talking

ahhhh thank you so much , i will try this later
  #4  
Old 16-Sep-2003, 20:12
Mvpsandy Mvpsandy is offline
New Member
 
Join Date: Sep 2003
Posts: 5
Mvpsandy is an unknown quantity at this point
i'm back and i have fixed alot of my errors .. but i am still having problems.... am i even on the right track? my code is below

CPP / C++ / C Code:
#include <iostream>

using std::cout;

using std::cin;

using std::end1;

int main ()

{

    int num; // watch you are only given single variables



std::cout<<" Enter A 5-Digit Number\n"; //prompt

std::cin >> num1 (54321 - (54321 % 10000 ) / 10000 >> num2 (4321 - (4321 %1000 ) / 1000 >> num3 (321 - ( 321%100) / 100 >> num4 (21 - ( 21 % 10 ) / 10 >> num5 ( 1 - ( 1 % 1 ) / 1 ; 



return 0; 

} 


sandra
  #5  
Old 16-Sep-2003, 21:39
OpenOutsource OpenOutsource is offline
New Member
 
Join Date: Jun 2003
Location: England
Posts: 16
OpenOutsource is an unknown quantity at this point
It may be better to show this in a long winded way, but in a way that you can probably follow the logic.

Take the number you input, say for example you entered 75364 then take away the remainder after dividing by 10000 and put it into num2

num2 = num1 % 10000; // num2 will contain 5364

Take this away from your original number to make it divisible by 10000

num1 = num1 - num2; // num1 will contain 70000 from (75364-5364)

Divide it by 10000

num1 = num1 / 10000; // num1 will now contain 7


Now work on the next step

num3 = num2 % 1000; // num3 will contain 364
num2 = num2 - num3; // num2 will contain 5000 from (5364 - 364)
num2 = num2 / 1000; // num will now contain 5


Next step
num4 = num3 % 100; // num4 will contain 64
num3 = num3 - num4; // num3 will contain 300 from (364 - 64)
num3 = num3 / 100; // num3 will now contain 3

Next step
num5 = num4 % 100; // num5 will contain 4
num4 = num4 - num3; // num4 will contain 60 from (64 - 4)
num4 = num4 / 10; // num4 will now contain 6

So each variable holds
num1 is 7
num2 is 5
num3 is 3
num4 is 6
num5 is 4


So the program segment without comments is
num2 = num1 % 10000;
num1 = num1 - num2;
num1 = num1 / 10000;
num3 = num2 % 1000;
num2 = num2 - num3;
num2 = num2 / 1000;
num4 = num3 % 100;
num3 = num3 - num4;
num3 = num3 / 100;
num5 = num4 % 100;
num4 = num4 - num3;
num4 = num4 / 10;

When you have mastered that you may wish to use a loop to take care of 'any' number of digits entered.

Another method would be to treat the input as a string and just convert each character in the string.

When all that becomes easy you could try writing the whole program in one line thereby making it a bit unreadable. There used to be contests in this some years back, it was called 'Obfuscated C' where you would have the programs actually rewrite themselves

Hope that helps,

Or indeed if it is correct

Shawn
  #6  
Old 16-Sep-2003, 22:21
Mvpsandy Mvpsandy is offline
New Member
 
Join Date: Sep 2003
Posts: 5
Mvpsandy is an unknown quantity at this point
thank you.... i read your responce and i totally understand what you are saying... but for some reason i cannot get my program to work still ..... i dont know i guess this is a lost cause. this is my first week learning c++ and i feel like pulling out my hair please tell me it gets better? also after reading your responce i dont know where to input the info you posted. I keep getting long error messages..... i guess im back at square one... i am sorry for buggin you guys ... but i really want to learn c++

Sandra
  #7  
Old 16-Sep-2003, 22:34
OpenOutsource OpenOutsource is offline
New Member
 
Join Date: Jun 2003
Location: England
Posts: 16
OpenOutsource is an unknown quantity at this point
It does get better - you will see. Keep going at it. The best way to learn anything is by the mistakes you can make. It would be just great though if there was something that would come along and tell you exactly what it was.

Where are the errors?
  #8  
Old 16-Sep-2003, 22:37
Mvpsandy Mvpsandy is offline
New Member
 
Join Date: Sep 2003
Posts: 5
Mvpsandy is an unknown quantity at this point
thanks for your help this evening.... i dont remember what exactly it said ( i just logged out ) ....... i appreciate you taking the time to help me, and your possitive feedback I think i am going to take a break for tonight because i have been working on this 1 program for hours ..... i think if i get some rest i will be able to start again tomorrow fresh. I will post what the errors were tomorrow ... have a good night


thanks again

Sandra~
  #9  
Old 16-Sep-2003, 22:42
OpenOutsource OpenOutsource is offline
New Member
 
Join Date: Jun 2003
Location: England
Posts: 16
OpenOutsource is an unknown quantity at this point
OK

A great thing about C or C++ is when you get loads of errors don't worry too much. They are most likely caused by the first. It is usually the first error that is important to fix, when you do this the others often just disappear.

Or am I being a little too positive
  #10  
Old 17-Sep-2003, 09:39
Dariklar Dariklar is offline
New Member
 
Join Date: Sep 2003
Location: Illinois
Posts: 9
Dariklar is on a distinguished road
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
	int num1, num2, num3, num4, num5;
	cout << "Please enter a 5-digit number.\n";
	cout << "I will then report to you the 5 digits with spaces inbetween them.\n";
	cin >> num1;
    num2 = num1 % 10000;
	num1 -= num2;
	num1 /= 10000;
	num3 = num2 % 1000;
	num2 -= num3;
	num2 /= 1000;
	num4 = num3 % 100; 
	num3 -= num4; 
	num3 /= 100; 
	num5 = num4 % 10;
	num4 -= num3;
	num4 /= 10;

	cout << "Your new number, with spaces, is " << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << endl;
	return 0;
}

Here is a program that does what you want.
__________________
Dariklar, 63 human monk
Migboaf, 64 dwarvish cleric
Xegony, EverQuest.
http://dariklar.googlepages.com
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with calendar program mike3340 C++ Forum 0 18-Nov-2003 20:25
error during program rjd72285 C++ Forum 0 11-Nov-2003 18:49
Why doesnt my form work correctly? rhino1616 Web Design Forum 2 06-Nov-2003 17:21
How do web redirection scripts work? rhino1616 Web Design Forum 9 27-Oct-2003 09:47
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.