如何将char c [0]传输到int

时间:2019-04-18 00:00:01

标签: c++ atoi

我用getline读取了一个文件,并且想要将char c [0]传输到int,我遇到了一个错误。

error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
g[n].chicken = atoi(c[0]);
                       ^

在assign.cpp中包含的文件中:5:0: /usr/include/stdlib.h:147:12:错误:初始化“ int atoi(con​​st char *)”的参数1 [-fpermissive]  extern int atoi(con​​st char * __ nptr)

 24        int g;
 25        string str;
 26        while(getline(file,str)){
 27           
 28           const char* ct = str.c_str();
 29           char c[5];
 30           strcpy(c,ct);
 31        
 32
 33           g = atoi(c[0]);

1 个答案:

答案 0 :(得分:0)

atoi的声明如下:// Initialize Firebase var config = { apiKey: "AIzaSyBZHH97LQ42n7j0fBjKR7LCLXkeDq-L7iw", authDomain: "pro-core-169202.firebaseapp.com", databaseURL: "https://pro-core-169202.firebaseio.com", projectId: "pro-core-169202", storageBucket: "pro-core-169202.appspot.com", messagingSenderId: "58388231689" }; firebase.initializeApp(config); let dataRef = firebase.database().ref("data"); document.getElementById("patiatint").addEventListener("submit", submitform); function submitform(e) { e.preventDefault(); let name = getInputVal("name"); let medicName = getInputVal("medic-name"); let medicTime1 = getInputVal("medic-time1"); let medicTime2 = getInputVal("medic-time2"); let notes = getInputVal("notes"); saveData(name, medicName, medicTime1, medicTime2, notes); } function getInputVal(id) { return document.getElementById(id).value; } function saveData(name, medicName, medicTime1, medicTime2, notes) { let newDataRef = dataRef.push(); newDataRef.set({ name: name, medicName: medicName, medicTime1: medicTime1, medicTime2: medicTime2, notes: notes }); } function amTime() { let amTime = document.getElementById('am'); if (amTime.checked == true) { document.getElementById("medic-time2").value = "AM"; } if (amTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function bmTime() { let bmTime = document.getElementById('bm'); if (bmTime.checked == true) { document.getElementById("medic-time2").value = "PM"; } if (bmTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function oneTime() { let oneTime = document.getElementById('one'); if (oneTime.checked == true) { document.getElementById("medic-time1").value = "1:00"; } if (oneTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function twoTime() { let twoTime = document.getElementById('two'); if (twoTime.checked == true) { document.getElementById("medic-time1").value = "2:00"; } if (twoTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function threeTime() { let threeTime = document.getElementById('three'); if (threeTime.checked == true) { document.getElementById("medic-time1").value = "3:00"; } if (threeTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function fourTime() { let fourTime = document.getElementById('four'); if (fourTime.checked == true) { document.getElementById("medic-time1").value = "4:00"; } if (fourTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function fiveTime() { let fiveTime = document.getElementById('five'); if (fiveTime.checked == true) { document.getElementById("medic-time1").value = "5:00"; } if (fiveTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function sixTime() { let sixTime = document.getElementById('six'); if (sixTime.checked == true) { document.getElementById("medic-time1").value = "6:00"; } if (sixTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function sevenTime() { let sevenTime = document.getElementById('seven'); if (sevenTime.checked == true) { document.getElementById("medic-time1").value = "7:00"; } if (sevenTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function eightTime() { let eightTime = document.getElementById('eight'); if (eightTime.checked == true) { document.getElementById("medic-time1").value = "8:00"; } if (eightTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function nineTime() { let nineTime = document.getElementById('nine'); if (nineTime.checked == true) { document.getElementById("medic-time1").value = "9:00"; } if (nineTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function tenTime() { let tenTime = document.getElementById('ten'); if (tenTime.checked == true) { document.getElementById("medic-time1").value = "10:00"; } if (tenTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function elevenTime() { let elevenTime = document.getElementById('eleven'); if (elevenTime.checked == true) { document.getElementById("medic-time1").value = "11:00"; } if (elevenTime.checked == false) { document.getElementById("medic-time1").value = ""; } } function twelveTime() { let twelveTime = document.getElementById('twelve'); if (twelveTime.checked == true) { document.getElementById("medic-time1").value = "12:00"; } if (twelveTime.checked == false) { document.getElementById("medic-time1").value = ""; } }

您需要

int atoi(const char* buffer)

因为c是指向字符数组的指针。数组c [5]由5个字节组成。 c [0]是第一个字节。编译器将c本身解释为指向第一个字节的指针。

因此,atoi()需要一个指向字符数组的指针。您将第一个字符传递给了它。

使用std :: getline()检索用户输入是一个好主意。学习使用stringstream从getline()中解析项目。一旦擅长,您将犯的错误更少:

g = atoi(c);