遊客:  註冊 | 登錄 | 幫助





標題: [求助] C++ programming
  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2009-2-2 12:43 PM  資料  個人空間  短訊  加為好友 
C++ programming

I got a problem here....hopefully got some1 teach me how to solve the problem...here is the question

#include <stdio.h>
#include <cstdlib>
#include <string.h>

int main()
{   float n1 ;
    char str1[10],str2[10],str3[10],str4[10];

         printf("Enter sentence like ¡°x grams in kilograms ?\n");
                           scanf("%f %s %s %s %s",&n1,str1,str2,str3,str4);
         
         int strcmp(const char *grams  ,const char *kilograms);
            if(1 grams = 1*0.001 kilograms);
                 printf("%f %s = %f %s\n",n1,str1,str2,str3);

        system("pause");
        return 0;
}

the output needed is :
Enter sentence like “x grams in kilograms ?”: 10 kilograms in grams ?
10 kilograms = 10000 grams.

i donno how to use the strcmp. can any1 teach me ??  
>> if(1 grams = 1*0.001 kilograms);
                 printf("%f %s = %f %s\n",n1,str1,str2,str3);

how to put the 1 grams = 1*0.001 answer to printf ?
i mean 1 grams is 0.001 kg right ? how to put the answer into printf ?  cause i never store the answer ...CX



最後編輯: onlylonely : 2009-2-3 05:36 PM
頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2009-2-3 10:37 AM  資料  個人空間  短訊  加為好友 
Hi onlylonely.. long time!

First of all, please change the title of your thread to something that is more relevant to the contents, such as "C++ question".  That way, other experts might jump in and help.  

strcmp is for comparing two strings.  e.g.  strcmp(myStr1, myStr2)  If myStr1 and myStr2 are identical, it returns zero; else it returns the first position where they are different.  So, if myStr1 = "abcd" and myStr2 = "abcd", it returns zero;  if myStr1 = "abcd" and myStr2 = "abbk", it returns 2.  That's what I remembered but I could be wrong.

I don't quite understand your question so can you tell us more.  Is your program going to prompt user for a number in kilograms and it will calculate the grams then print something like "5 kilograms = 5000 grams"?

頂部



  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2009-2-3 05:39 PM  資料  個人空間  短訊  加為好友 
Ya , you are right . I need to convert the Kilograms to grams .
For example :
10 kilograms in grams ?
output : 10 kilograms = 10000 grams.

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2009-2-5 01:09 AM  資料  個人空間  短訊  加為好友 
回復 #3 onlylonely 的帖子

Then you do not need strcmp() I guess.  The steps should be something like the following:

1. prompt user for a numeric value using scanf
2. convert the input to grams and store the new value in a variable
3. display the output using printf

I would skip step 2 for now, just see if you can do step 1 and 3 so that it will prompt user for a value and then you output the same value.  For example:

x kilograms in grams ? 6
output: 6 kilograms

If you can get that to work, you got 80% done.  Tell us where you are stuck and we'll try to help.  

頂部

快速美言
           


當前時區 GMT+8, 現在時間是 2024-3-29 05:05 PM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.007479 second(s), 8 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver