Write a program that reads from the user a 5-digit whole number into a c-string and produces a single integer value from that and prints the resulting integer.
1
Expert's answer
2014-10-28T14:07:01-0400
#include <stdlib.h> #include <iostream> #include <stdio.h> #include <cstdlib> #include <cstdio> using namespace std; int atoi(char s[]) { int i, n; for(i = 0, n = 0; s[i] >= '0' && s[i] <= '9'; i++) n = n * 10 + ( s[i] - '0'); return n; } void CopyString (char *s, char *t) { while ((*s = *t) != '\0') { s++; t++; } } int main() { char c; int i = 0; int start_i = 2; char *s1 = new char [start_i]; char *s2 = new char [start_i]; cout<<"Enter the whole number: \n"; while ((c = getchar())!= '\n') { if (i > start_i-2) { *(s1 + i + 1) = '\0'; CopyString (s2,s1); ++start_i; s1 = new char[start_i]; CopyString(s1, s2); s2 = new char[start_i]; } *(s1+i) = c; i++; } *(s1 + i) = '\0'; delete []s2; int k = atoi(s1); cout<<"Number : "<<k<<endl;; system("pause"); return 0; }
Comments
Leave a comment