63 126
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #47988 in C++ for delaram

Question #47988

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.
Expert's answer
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions