CR LF lp chapter5

來源:互聯網
上載者:User

#include <stdio.h><br />#include <stdlib.h><br />char* menu[] = {<br />"a - add new record",<br />"d = delete record",<br />"q - quit",<br />NULL,<br />};<br />// int func(int a[]) // a is a pointer to the first element of an array<br />// int func(int* a) // is the same as the above one<br />// so I think the below one is equal to char** choices);<br />// int getchoice(char* greet, char* choices[]);<br />int getchoice(char* greet, char** choices);<br />int main()<br />{<br />int choice = 0;<br />do<br />{<br />choice = getchoice("please select an action", menu);<br />printf("you have chosen: %c/n", choice);<br />} while(choice != 'q');<br />exit(0);<br />}<br />int getchoice(char* greet, char** choices)<br />// int getchoice(char* greet, char* choices[])<br />{<br />int chosen = 0;<br />int selected;<br />char** option;<br />do {<br />printf("choice: %s/n", greet);<br />option = choices;<br />while (*option)<br />{<br />printf("%s/n", *option);<br />option++;<br />}<br />// when prog running, when user types 'a' and Enter key, prog see 'a' and<br /> // LF('/n')(10), not the character CR'/r'(13), unix and linux use a single<br />// LF as the end of a line not like in windows it use both the two characters<br />// but still strange happens, this program runs very well in both linux and<br />// windows OS.<br />do // if I typed 'a' and Enter in shell bash, there will be two characters in<br />{ // buffer, character 'a'(97) and character '/n'(10),<br />selected = getchar();<br />} while (selected == '/n'); // discard the '/n'<br />option = choices;<br />while (*option)<br />{<br />if (selected == *option[0])<br />{<br />chosen = 1;<br />break;<br />}<br />option++;<br />}<br />if (!chosen)<br />{<br />printf("incorrect choice, select again/n");<br />}<br />} while (!chosen);<br />return selected;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.