DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   songa rating prog (http://www.daniweb.com/forums/thread165845.html)

sinduja Jan 3rd, 2009 1:18 pm
songa rating prog
 
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i,j,t,s;
clrscr();
char sname[30],rating[60];
cout<<"enter no of songs in the list"<<endl;
cin>>n;
cout<<"enter song name and rating"<<endl;
for(i=1;i<=n;i++)
{
cin>>sname[i];
}
for(i=1;i<=n;i++)
{
cin>>rating[i];
}
cout<<"The top songs are in the order:"<<endl;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(rating[i]<rating[j])
{
t=rating[i];
s=sname[i];
rating[i]= rating[j];
sname[i]=sname[j];
rating[j]=t;
sname[j]=s;
}
}
}
for(i=1;i<=n;i++)
{

cout<<"Name:"<<sname[i]<<"rating:"<<rating[i]<<endl;
}

getch();
}



I am a beginner in C++ n am tryin to write a prog which gets the songs name n rating as input and list the songs with greater rating in the ascending, the problem is i could input oly one character as ip for song name and i am not aware of a soln,so geeks pls help n i welcome improvisations too:)

Freaky_Chris Jan 3rd, 2009 1:25 pm
Re: songa rating prog
 
http://www.daniweb.com/forums/announcement8-3.html
http://www.daniweb.com/forums/thread78223.html
http://www.catb.org/~esr/faqs/smart-questions.html

Have a nice day.
Chris

vmanes Jan 3rd, 2009 11:27 pm
Re: songa rating prog
 
Please use full words in your question, and follow the links given above to see how to better post questions here.

The main thing you need to study up on is string input and manipulation. Your array of characters is going to hold just one string (song title), not 30 of them.

Once that's done, in your sorting block, you're doing the poorest variation of selection sort, which usually works out to poorer than bubble sort. Look up code for that, and for the means to copy strings.

Lastly, your loop controls
 for( i = 1; i <=n; i++ )
are setting a bad habit that will come back to bite you eventually. Generally write loop for array handling as
 for( i = 0; i < n; i++ )
so that you use the first element of the array and will not go past its end ( assuming n elements in the array).


All times are GMT -4. The time now is 6:00 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC