Reputation: 319
I'm new in programming and iam from Tver. There is a problem in program. I don't know where. I am using input file and output file. So, i tried to debug program, but i failed I'm using a Visual Studio 2010. Thank you in advance.
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string.h>
using namespace std;
int num[100];
void outc(int s, int ss)
{int i,temp,numb[100],k,l,t;
temp=s; i=0;
while (temp>0)
{
numb[i]=temp%ss;
if (numb[i]>=10) numb[i]='A'-10+temp%16;
temp/=ss;
i++;}
l=i/2; t=0;
i--;
while (i>=l)
{
k=numb[t];
numb[t]=numb[i];
numb[i]=k;
t++;
i--;
}
FILE* fooo;
errno_t errorCodes=fopen_s(&fooo,"output.txt","w");
fprintf(fooo,"s%d= %d\n", ss, numb);
return;
}
int main()
{char c,strbuf[100],num[100];
char *res;
int k,s,i,temp,ost,s2,s8,s10,s16;
FILE* foo;
errno_t errorCode=fopen_s(&foo,"input.txt","r");
fgets(strbuf,1000,foo);
if(strbuf[strlen(strbuf)-1]=='b')
{
strncpy_s(strbuf, strbuf, strlen(strbuf)-1);
c=atoi(strbuf);
k=0;s=0;
while(c!=0)
s+=(c%10)*pow(2,k);
c/=10;
k++;
} else
if(strbuf[0]==0 && strbuf[1]!='x')
{i=0;;
do{
strbuf[i]=strbuf[i+1];
i++;
}while(i!=strlen(strbuf)-1);
c=atoi(strbuf);
k=0;s=0;
while(c!=0)
s+=(c%10)*pow(8,k);
c/=10;
k++;
} else
if(strbuf[0]=='0' && strbuf[1]=='x')
{i=0;k=strlen(strbuf);
do{
strbuf[i]=strbuf[i+2];
i++;
}while(i!=k);
puts(strbuf);
k=0;s=0;
for (i=strlen(strbuf)-1;i>=0; i--)
{
if (strbuf[i]>='A' && strbuf[i]<='F')
c=10+strbuf[i]-'A'; else c=strbuf[i]-'0';
printf("%d\n",c);
s+=c*pow(16,k);
k++;
}
} else s=atoi(strbuf);
printf("%d\n",s);
outc(s,2);
outc(s,8);
FILE* fooo;
errno_t errorCodep=fopen_s(&fooo,"output.txt","w");
fprintf(fooo,"s10= %d\n", s);
outc(s,16);
//if (temp%16>=10) num[len-1]='A'-10+temp%16;
//printf("s2= %d\ns8= %d\ns10= %d\ns16= %d\n", s2, s8, s, s16);
_getch();
return 0;
}
Upvotes: 2
Views: 401
Reputation: 25908
This has numerous issues:
You can't #include <iostream>
or have using namespace std
in a C program.
There are so many compiler specific things in here that it's going to be hard for most people to help you. You'll make your life a lot easier by writing in standard C. There's no way for me to compile this program to check what's wrong with it, for instance.
Your code is very hard to follow when you use variable names like k
, and s
, and s2
, and the like, and do things like FILE * foo
followed by FILE * fooo
. Your code is also just formatted horribly.
With strncpy_s(strbuf, strbuf, ...)
unless Microsoft is doing something really weird, here, you can't specify the same string as both the source and destination.
strtol()
is better here than atoi()
.
You're not closing any of the files you open, and you're not checking whether they did, in fact, open. Using the &
operator here: errorCode=fopen_s(&foo, ...
is highly suspicious, but again, you're using some non-standard function, so who knows.
Here: fprintf(fooo,"s%d= %d\n", ss, numb)
you tell fprintf()
to expect two int
s, but the last argument is an array.
Upvotes: 1