|
thank you finally I got it..
|
|
|
|
|
even though I pass fwrite(fout,((float)rand()/(float)(RAND_MAX)) *a,text);
it is generating the file.txt but null value on it..
|
|
|
|
|
#include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
void main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num!=0)
{
num=num/10;
i++;
}
for(j=i;j>0;j--)
{ multiplier=pow(10,j);
a[j]=num/multiplier;
num=num-(a[j]*multiplier);
}
for(j=0;j<i;j++)
sum=sum+a[j];
cout<<"The sum of the digits of the "<<i<<" digits number "<<num1<<" is:"<<sum;
getch();
}
|
|
|
|
|
If you want to iterate the digits, then why get the number as a number rather than as a string?
I'd get the string, iterate the digits, then get the numeric value of each digit, and then sum them.
That makes getting the number of digits simpler as well.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
Are you wanting a "digital root" algorithm?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
The 'quick fix' of your bugs produce the following program:
int main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num1!=0)
{
num1=num1/10;
i++;
}
num1 = num;
for(j=i-1;j>=0;j--)
{
multiplier=pow(10,j);
a[j]=num1/multiplier;
num1=num1-(a[j]*multiplier);
}
for(j=0;j<i;j++)
sum=sum+a[j];
cout<<"The sum of the digits of the "<<i<<" digits number "<<num<<" is:"<<sum << endl;
getchar();
}
However, I would rather write it this way:
int main()
{
long num, sum = 0;
cout<<"Enter the number:";
cin>>num;
while (num)
{
int remainder = num % 10;
sum += remainder;
num /= 10;
}
cout <<"The sum of the digits of is:" << sum << endl;
getchar();
}
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
modified 28-Jul-14 5:55am.
|
|
|
|
|
I'd like to remind you that you probably meant to write remainder , not reminder .
Good stuff though.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Stefan_Lang wrote: I'd like to remind you that you probably meant to write remainder , not reminder .
Thank you, fixed.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Hi,
Don't use so many loops unnecessarly, here is the one way to get your answer in optimistic way.
int main()
{
long num,num1,i=0,sum=0;
short remainder = 0;
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num!=0)
{
remainder = num % 10;
num = num / 10;
sum = sum + remainder;
i++;
}
cout<<"The sum of the digits of the "<
|
|
|
|
|
This method does not work:
lvi.mask = LVIF_TEXT;
lvi.pszText = "one";
m_ListCtrl.InsertItem(&lvi);
lvi.pszText = "two";
lvi.iSubItem = 1;
m_ListCtrl.InsertItem(&lvi);
This method does not work:
for (int nI = 0; nI < m_strarrayOtherImgFiles.GetCount(); nI++)
{
pOtherImgsList->SetItemText(nI, 0, m_strarrayOtherImgFiles.GetAt(nI));
}
Nothing seems to work.
|
|
|
|
|
Hello,
First, see the topic - 'How to ask Questions' in this forum.
Second, you are missing lvi.iItem I think. Try adding this and see.
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
lvi.iSubItem = 0;
lvi.pszText = "one";
m_ListCtrl.InsertItem(&lvi);
lvi.pszText = "two";
lvi.iSubItem = 1;
m_ListCtrl.InsertItem(&lvi);
Regards,
A. Gopinath.
|
|
|
|
|
|
I have been reading the documentation - where else do you think I obtained those methods that I have been trying.
This is what I have tried precisely.
And function
int InsertItem(
int nItem,
LPCTSTR lpszItem
,as described here (http://msdn.microsoft.com/en-us/library/8b9s12fc.aspx[^]), DOES NOT WORK The function does nothing. WHY?
Can you only add items to a CListCtrl via
int InsertItem(
const LVITEM* pItem
); and the function that I have been using is in fact defunct?
void CImageDialog::DDX_List(CDataExchange* pDX, UINT uListCtrlID, CStringArray& rarrayOtherImgFiles)
{
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(uListCtrlID);
int nI = 0;
CString strItem;
ASSERT_VALID(pListCtrl);
if (pDX->m_bSaveAndValidate)
{
m_arrayOtherImageFiles.RemoveAll();
for (nI = 0; nI < pListCtrl->GetItemCount(); nI++)
{
strItem = pListCtrl->GetItemText(nI, 0);
m_arrayOtherImageFiles.Add(strItem);
}
}
else
{
pListCtrl->DeleteAllItems();
for (nI = m_arrayOtherImageFiles.GetCount() - 1; nI >= 0; nI--)
{
pListCtrl->InsertItem(1, m_arrayOtherImageFiles.GetAt(nI));
}
}
}
void CImageDialog::DoDataExchange(CDataExchange* pDX)
{
CMyDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_HEADER_IMG, m_strHeaderImageFile);
DDX_Text(pDX, IDC_EDIT_NAV_IMG1, m_strNavImg1File);
DDX_Text(pDX, IDC_EDIT_NAV_IMG2, m_strNavImg2File);
DDX_Text(pDX, IDC_EDIT_OTHER_IMG_FILES, m_strOtherImgFiles);
DDX_List(pDX, IDC_LIST_OTHER_IMG_FILES, m_arrayOtherImageFiles);
}
|
|
|
|
|
Of course the function works, it is used in List controls all over the world. It is more likely that your code is doing something wrong. For a start you are not checking the return code to see whether it succeeded or not, and if not, why. What is the current value of the index nI , and what is being returned by the call to m_arrayOtherImageFiles.GetAt(nI) ? Use your debugger to trace through your code to diagnose the problem.
|
|
|
|
|
how to generate text file for a program output?
|
|
|
|
|
|
Quote:
#include <sys types.h=""> // for scandir
#include <sys dir.h=""> // for alphasort and structure dirent
#include <sys param.h=""> // for MAXPATHLEN and getwd
#include <stdio.h>
#include "Utility.c"
#include "Utility.h"
//#include <stdlib.h>
#include <math.h>
#define FALSE 0
#define TRUE !FALSE
extern int alphasort();// extern extend the visiblity of the function alphasort
//alphasort used as the comparison function for the scandir() line no 24 function to sort the directory
char pathname[MAXPATHLEN];
main() { int count,i;
struct direct **files;//pointer to a pointer ,which point to the object call files here
int file_select();
//printf("\n enter the file ")
if (getwd(pathname) == NULL ) // return absolute file name of current directory
{ printf("Error getting path\n");
exit(0);
}
printf("Current Working Directory = %s\n",pathname);
count =
scandir(pathname, &files, file_select, alphasort);//scandir returns the current directory (.) and the directory above this (..) as well as all files so we need to check
/* If no files found, make a non-selectable menu item */
if (count <= 0)
{ printf("No files in this directory\n");
exit(0);
}
printf("Number of files = %d\n",count);
for (i=1;i<count+1;++i){
printf("\n%s\n="" ",files[i-1]-="">d_name);
int SizeOfData,j;
float *data;
// float *text;
char *wavFileName=".wav";
FILE *fout;
/*if (argc < 2) {
printf("\nUsage : SID_IITG waveFileName \n");
return -1;
}*/
//wavRead(argv[1],&SizeOfData) ;
data=wavRead(files[i-1]->d_name,&SizeOfData) ; //func call to read wave file
// text =wavRead(files[i-1]->d_name,&SizeOfData) ;
printf("\nReading Done\n");
fout = fopen ("dataSamples","w"); // output file
// fout = fopen ("datasamples.txt","w");
j=0;
while (j < SizeOfData) {
//printf("%f\n",data[j]);
fprintf(fout, "%f\n",data[j]);
// fprintf(fout, "%f\n",text[j]);
j++;
}
}
printf("\n"); /* flush buffer */
}
int file_select(struct direct *entry)
{ char *ptr;
char *rindex(char *s, char c);
if ((strcmp(entry->d_name, ".") == 0) ||
(strcmp(entry->d_name, "..") == 0))
return (FALSE);
// checking of its extension
ptr = rindex(entry->d_name, '.');
if ((ptr != NULL) &&
((strcmp(ptr, ".wav") == 0) ))/*||
(strcmp(ptr, ".txt") == 0) ||
(strcmp(ptr, ".c") == 0) )) */
return (TRUE);
else
return (FALSE);
}
|
|
|
|
|
And? Do you have a question?
|
|
|
|
|
<blockquote class="quote"><div class="op">Quote:</div>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#include "Utility.c"
#include "Utility.h"
#include <math.h>
#define FALSE 0
#define TRUE !FALSE
extern int alphasort();
char pathname[MAXPATHLEN];
main() { int count,i;
struct direct **files;
int file_select();
if (getwd(pathname) == NULL )
{ printf("Error getting path\n");
exit(0);
}
printf("Current Working Directory = %s\n",pathname);
count =
scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{ printf("No files in this directory\n");
exit(0);
}
printf("Number of files = %d\n",count);
for (i=1;i<count+1;++i){
printf("\n%s\n ",files[i-1]->d_name);
int SizeOfData,j;
float *data;
char *wavFileName=".wav";
FILE *fout;
data=wavRead(files[i-1]->d_name,&SizeOfData) ;
printf("\nReading Done\n");
fout = fopen ("dataSamples","w");
j=0;
while (j < SizeOfData) {
fprintf(fout, "%f\n",data[j]);
j++;
}
}
printf("\n");
}
int file_select(struct direct *entry)
{ char *ptr;
char *rindex(char *s, char c);
if ((strcmp(entry->d_name, ".") == 0) ||
(strcmp(entry->d_name, "..") == 0))
return (FALSE);
ptr = rindex(entry->d_name, '.');
if ((ptr != NULL) &&
((strcmp(ptr, ".wav") == 0) ))
return (TRUE);
else
return (FALSE);
}
</blockquote>
// along with the output if what shall i do for getting text file of the output also.?
|
|
|
|
|
You should not use fprintf to write binary data (.wav files); use WriteFile as I suggested in my previous post. You can use fprintf to create a text file containing the names of all your files.
|
|
|
|
|
do this WriteFile function work in ubuntu ?
|
|
|
|
|
My apologies, you need fwrite[^] in Linux.
|
|
|
|
|
Your program could simply write its output to console (e.g. using printf function, or cout object), then (on program invocation) you may redirect the standard output to a text file, e.g.
myprogram > myfile.txt
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Actually without giving command I want to generate itself..that why but Thanks anyhow..
|
|
|
|
|
By the way how to use this function?
|
|
|
|
|