| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
splitting inputs....im so dumb =)
im so new at this so this is probably a really easy question for you all!!!
basically a user inputs a 7 digit number (eg..1234567) and i need to split it into 2 parts part1=1234 part2=567 how do i code this??? any help would be great! |
|
#2
|
||||
|
||||
|
input the value as a string, then seperate the string.... ie:
Code:
char one[5],two[5]; gets(one); strcpy(two,&one[5]); one[5] = 0; that's ugly, but the general idea you need.... also, if anyone else is gong to use the program, you will also need to add a check to ensure the length of the string input....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
or do this:
Code:
#include <iostream>
using namespace std;
int main()
{
int my8digitnumber;
int first4digits, last4digits;
cout << "Enter an eight-digit number: ";
cin >> my8digitnumber;
first4digits = my8digitnumber / 10000; //since first4digits is an integer variable, the decimal portion will be stripped off
last4digits = my8digitnumber % 10000; // the % operator, called modulus, performs division returns the remainder
cout << "First 4 digits: " << first4digits << ". Last 4 digits: " << last4digits << "." << endl;
cin.ignore();
cin.get();
return 0;
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > splitting inputs....im so dumb =) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|